- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一些在我的存储库层中使用 EF 的模块类库 API。为了使其中的任何一个起作用,我需要在每个类库中有一个 dbcontext 类。但是当我需要在每个模块中引用一个类时会发生什么?例如,我有一个用户模块,其数据库上下文包括:
然后我有一个位置模块,其中包括:
然后说第三个设备模块,它有:
后两者仍然需要对用户的引用,这几乎是每个模块不可或缺的一部分。但是我不能将两个单独的用户类添加到指向同一个数据库的两个上下文中,它们可能会变得不同步。因此,显而易见的解决方案就是让后两个模块需要用户模块,而那些需要用户的模块中的任何类都只引用用户 ID。这会破坏规范化,因为它不是外键,所以我不确定这个想法有多好。
我突然想到的另一种可能性是让每个模块的 dbcontext 使用一个接口(interface),并允许使用该模块的人声明他们自己的 dbcontext 并实现所有这些成员,但我不确定这是否可行。
我基本上只是想制作一组类库模块,这些模块定义了一组可供其他程序员使用的通用类和 API 调用,同时使用 EF 作为基础,目的是将它们全部存储在一个数据库中。但我不太确定如何通过 DbContexts 的工作方式来实现这一点。当您有多个模块需要同一个对象时会发生什么?
最佳答案
您所代表的三个上下文通常匹配 Bounded Contexts按照 Domain-driven design 中的设计,正如 Steeve 正确指出的那样。
显然有多种方法可以实现这种情况,每种方法都各有利弊。
我建议采用两种方法来尊重领域驱动设计的最佳实践并具有很大的灵 active 。
方法 #1:软分离
我在第一个限界上下文中定义了一个 User
类,在第二个限界上下文中定义了一个表示对用户的引用的接口(interface)。
让我们定义用户:
class User
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
}
其他引用用户的模型实现IUserRelated
:
interface IUserRelated
{
[ForeignKey(nameof(User))]
Guid UserId { get; }
}
设计模式建议不要直接链接来自两个分离的有界上下文的两个实体,而是存储它们各自的引用。
Building
类如下所示:
class Building : IUserRelated
{
[Key]
public Guid Id { get; set; }
public string Location { get; set; }
public Guid UserId { get; set; }
}
如您所见,Building
模型只知道 User
的引用。尽管如此,该接口(interface)充当外键并限制插入到此 UserId
属性中的值。
现在让我们定义数据库上下文...
class BaseContext<TContext> : DbContext where TContext : DbContext
{
static BaseContext()
{
Database.SetInitializer<TContext>(null);
}
protected BaseContext() : base("Demo")
{
}
}
class UserContext : BaseContext<UserContext>
{
public DbSet<User> Users { get; set; }
}
class BuildingContext : BaseContext<BuildingContext>
{
public DbSet<Building> Buildings { get; set; }
}
以及初始化数据库的数据库上下文:
class DatabaseContext : DbContext
{
public DbSet<Building> Buildings { get; set; }
public DbSet<User> Users { get; set; }
public DatabaseContext() : base("Demo")
{
}
}
最后,创建用户和建筑物的代码:
// Defines some constants
const string userName = "James";
var userGuid = Guid.NewGuid();
// Initialize the db
using (var db = new DatabaseContext())
{
db.Database.Initialize(true);
}
// Create a user
using (var userContext = new UserContext())
{
userContext.Users.Add(new User {Name = userName, Id = userGuid});
userContext.SaveChanges();
}
// Create a building linked to a user
using (var buildingContext = new BuildingContext())
{
buildingContext.Buildings.Add(new Building {Id = Guid.NewGuid(), Location = "Switzerland", UserId = userGuid});
buildingContext.SaveChanges();
}
方法 #2:硬分离
我在每个限界上下文中定义了一个 User
类。接口(interface)强制执行公共(public)属性。 Martin Fowler 说明了这种方法,如下所示:
用户限界上下文:
public class User : IUser
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
}
public class UserContext : BaseContext<UserContext>
{
public DbSet<User> Users { get; set; }
}
构建限界上下文:
public class User : IUser
{
[Key]
public Guid Id { get; set; }
}
public class Building
{
[Key]
public Guid Id { get; set; }
public string Location { get; set; }
public virtual User User { get; set; }
}
public class BuildingContext : BaseContext<BuildingContext>
{
public DbSet<Building> Buildings { get; set; }
public DbSet<User> Users { get; set; }
}
在这种情况下,在 BuildingContext
中拥有一个 Users
属性是完全可以接受的,因为用户也存在于建筑物的上下文中。
用法:
// Defines some constants
const string userName = "James";
var userGuid = Guid.NewGuid();
// Create a user
using (var userContext = new UserContext())
{
userContext.Users.Add(new User { Name = userName, Id = userGuid });
userContext.SaveChanges();
}
// Create a building linked to a user
using (var buildingContext = new BuildingContext())
{
var userReference = buildingContext.Users.First(user => user.Id == userGuid);
buildingContext.Buildings.Add(new Building { Id = Guid.NewGuid(), Location = "Switzerland", User = userReference });
buildingContext.SaveChanges();
}
使用 EF 迁移非常简单。用户限界上下文的迁移脚本(由 EF 生成):
public partial class Initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Users",
c => new
{
Id = c.Guid(nullable: false),
Name = c.String(),
})
.PrimaryKey(t => t.Id);
}
public override void Down()
{
DropTable("dbo.Users");
}
}
构建限界上下文的迁移脚本(由 EF 生成)。我必须删除表 Users
的创建,因为另一个有界上下文负责创建它。在为模块化方法创建表之前,您仍然可以检查该表是否不存在:
public partial class Initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Buildings",
c => new
{
Id = c.Guid(nullable: false),
Location = c.String(),
User_Id = c.Guid(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Users", t => t.User_Id)
.Index(t => t.User_Id);
}
public override void Down()
{
DropForeignKey("dbo.Buildings", "User_Id", "dbo.Users");
DropIndex("dbo.Buildings", new[] { "User_Id" });
DropTable("dbo.Users");
DropTable("dbo.Buildings");
}
}
为这两个上下文应用Upgrade-Database
,您的数据库就准备好了!
编辑关于在 User
类中添加新属性的 OP 请求。
当有界上下文向类 User
添加新属性时,它会在后台逐渐添加一个新列。它不会重新定义整个表。这就是此实现也非常通用的原因。
下面是一个迁移脚本示例,其中一个新属性 Accreditation
添加到限界上下文 Building
中的类 User
:
public partial class Accreditation : DbMigration
{
public override void Up()
{
AddColumn("dbo.Users", "Accreditation", c => c.String());
}
public override void Down()
{
DropColumn("dbo.Users", "Accreditation");
}
}
关于c# - 如何首先在 EF 代码中处理一个数据库在多个数据库上下文中使用的一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39456105/
我的问题是如何在 python 中创建一个简单的数据库。我的例子是: User = { 'Name' : {'Firstname', 'Lastname'}, 'Address' : {'Street
我需要创建一个与远程数据库链接的应用程序! mysql 是最好的解决方案吗? Sqlite 是唯一的本地解决方案吗? 我使用下面的方法,我想知道它是否是最好的方法! NSString *evento
给定两台 MySQL 服务器,一台本地,一台远程。两者都有一个包含表 bohica 的数据库 foobar。本地服务器定义了用户 'myadmin'@'%' 和 'myadmin'@'localhos
我有以下灵活的搜索查询 Select {vt:code},{vt:productcode},{vw:code},{vw:productcode} from {abcd AS vt JOIN wxyz
好吧,我的电脑开始运行有点缓慢,所以我重置了 Windows,保留了我的文件。因为我的大脑还没有打开,所以我忘记事先备份我的 MySQL 数据库。我仍然拥有所有原始文件,因此我实际上仍然拥有数据库,但
如何将我的 Access 数据库 (.accdb) 转换为 SQLite 数据库 (.sqlite)? 请,任何帮助将不胜感激。 最佳答案 1)如果要转换 db 的结构,则应使用任何 DB 建模工具:
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
我想在相同的 phonegap 应用程序中使用 android 数据库。 更多说明: 我创建了 phonegap 应用程序,但 phonegap 应用程序不支持服务,所以我们已经在 java 中为 a
Time Tracker function clock() { var mytime = new Date(); var seconds
我需要在现有项目上实现一些事件的显示。我无法更改数据库结构。 在我的 Controller 中,我(从 ajax 请求)传递了一个时间戳,并且我需要显示之前的 8 个事件。因此,如果时间戳是(转换后)
我有一个可以收集和显示各种测量值的产品(不会详细介绍)。正如人们所期望的那样,显示部分是一个数据库+建立在其之上的网站(使用 Symfony)。 但是,我们可能还会创建一个 API 来向第三方公开数据
我们将 SQL Server 从 Azure VM 迁移到 Azure SQL 数据库。 Azure VM 为 DS2_V2、2 核、7GB RAM、最大 6400 IOPS Azure SQL 数据
我正在开发一个使用 MongoDB 数据库的程序,但我想问在通过 Java 执行 SQL 时是否可以使用内部数据库进行测试,例如 H2? 最佳答案 你可以尝试使用Testcontainers Test
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 此问题似乎与 a specific programming problem, a sof
我正在尝试使用 MSI 身份验证(无需用户名和密码)从 Azure 机器学习服务连接 Azure SQL 数据库。 我正在尝试在 Azure 机器学习服务上建立机器学习模型,目的是我需要数据,这就是我
我在我的 MySQL 数据库中使用这个查询来查找 my_column 不为空的所有行: SELECT * FROM my_table WHERE my_column != ""; 不幸的是,许多行在
我有那个基地:http://sqlfiddle.com/#!2/e5a24/2这是 WordPress 默认模式的简写。我已经删除了该示例不需要的字段。 如您所见,我的结果是“类别 1”的两倍。我喜欢
我有一张这样的 table : mysql> select * from users; +--------+----------+------------+-----------+ | userid
我有表: CREATE TABLE IF NOT EXISTS `category` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL
我是一名优秀的程序员,十分优秀!