[英]Create a database relationship between 2 entities in different Entity Framework DbContexts
我有两个位于不同程序集中的对象(UserProfile和Login),我必须在SQL Server中创建它们之间的关系(我不在乎DbContext关系,但我想确保两个对象之间的数据库参照完整性各自的表格)。
最简单的方法是更新数据库(我首先使用代码)并运行一个alter table创建关系,但是我认为这种方法有点脏(即使在Up方法中使用SqlResource)。
我尝试使用HasOptional和Map方法创建这种关系失败(流利的api说,当我尝试类似“ HasOptional(a => a.IdUsuario)...”之类的内容时,ProfileId不是引用类型)。
// UserContext's Classes (Assembly 'A')
public class UserProfile
{
public int Id { get; set; } // PK
// some properties omitted
}
public class UserContext : DbContext
{
DbSet<UserProfile> UsersProfiles { get; set; }
}
// SecurityContext's Classes (Assembly 'B')
public class Login
{
public int Id { get; set; }
public string UserName { get; set; }
public int ProfileId { get; set; } // UserProfile's FK
}
public class SecurityContext : DbContext
{
DbSet<Login> Logins { get; set; }
}
我试图引用这两个类,包括导航属性,并使用“ modelBuilder.Ignore <>”来选择我要在每个上下文中真正进行同步的类,但这最终导致了循环引用问题。
我收到消息“导航属性'UserProfile'不是类型'登录'的声明的属性。请验证它尚未明确从模型中排除,并且它是有效的导航属性。” 如果尝试创建仅用于映射目的的伪造类。
我想念什么吗?
是的,您缺少一些要点。
UserProfile
是一个弱实体 ,它是Login
的依赖项。 关系是1:1。 1个Login
包含1个UserProfile
。
您的课程应如下所示:
public class UserProfile
{
//UserProfile does not have its own Id,
//Its Id is the LoginId, which is the primary key and a foreign key
//It ensures the 1:1 relationship
public int LoginId { get; set; } // PK and FK
// some properties omitted
public Login Login { get; set; }
}
public class Login
{
public int LoginId { get; set; }
public string UserName { get; set; }
//Login does not have UserProfile fk
//because it is the Principal of the relationship
public UserProfile Profile { get; set; }
}
制图:
modelBuilder.Entity<UserProfile>()
.HasKey(i => i.LoginId);
modelBuilder.Entity<Login>()
.HasKey(i => i.LoginId);
modelBuilder.Entity<Login>()
.HasRequired(i => i.Profile)
.WithRequiredPrincipal(i => i.Login)
.WillCascadeOnDelete(false);
产生的迁移:
CreateTable(
"dbo.Logins",
c => new
{
LoginId = c.Int(nullable: false, identity: true),
UserName = c.String(),
})
.PrimaryKey(t => t.LoginId);
CreateTable(
"dbo.UserProfiles",
c => new
{
LoginId = c.Int(nullable: false),
})
.PrimaryKey(t => t.LoginId)
.ForeignKey("dbo.Logins", t => t.LoginId)
.Index(t => t.LoginId);
编辑
由于Login
不属于域程序集,因此Login
是弱实体 。 Login
具有UserProfile
的依赖项。
public class UserProfile
{
public int UserProfileId { get; set; } // PK
// some properties omitted
// we don't have Login property here
}
public class Login
{
//login pk must be userprofile FK
//so it ensures the 1:1 relationship
public int UserProfileId { get; set; } //PK
public string UserName { get; set; }
public UserProfile Profile { get; set; }// UserProfile's FK
}
制图:
modelBuilder.Entity<Login>()
.HasKey(i => i.UserProfileId);
modelBuilder.Entity<Login>()
.HasRequired(i => i.Profile)
.WithRequiredDependent();
产生的迁移
CreateTable(
"dbo.UserProfiles",
c => new
{
UserProfileId = c.Int(nullable: false, identity: true),
})
.PrimaryKey(t => t.UserProfileId);
CreateTable(
"dbo.Logins",
c => new
{
UserProfileId = c.Int(nullable: false),
UserName = c.String(),
})
.PrimaryKey(t => t.UserProfileId)
.ForeignKey("dbo.UserProfiles", t => t.UserProfileId)
.Index(t => t.UserProfileId);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.