问题描述
- 客户
- 公司
- 等等
这里明显的问题是,正确的一对一表会将主体的PK(例如,客户的PK等)用作地址键,而使用多个表将导致地址键重复,因此我想使用组合键作为参考。
这是基本的一对一,只有实体类型:
客户类别
public class Customers
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity),Column(Order = 1),ForeignKey("Address")]
public int Id { get; set; }
public string CustomerName { get; set; }
public virtual Addresses Address { get; set; }
}
地址类别
public class Addresses : AuditBase
{
[Key,Column(Order = 1)]
public int Id { get; set; }
[required]
[StringLength(500)]
public string Address1 { get; set; }
[StringLength(500)]
public string Address2 { get; set; }
[required,MaxLength(100)]
[StringLength(150)]
public string City { get; set; }
[required,MaxLength(10)]
[StringLength(2)]
public string State { get; set; }
[required]
[StringLength(5)]
public string Zip { get; set; }
#endregion Properties
}
用于Cascade Delete的Fluent API实现
public static void buildModel(ref DbModelBuilder modelBuilder)
{
modelBuilder.Initializeusermodel();
modelBuilder.Entity<Customers>()
.HasOptional(x => x.Address)
.Withrequired()
.WillCascadeOnDelete(true);
}
如您所见,迁移会正确呈现地址表。
CreateTable(
"dbo.Addresses",c => new
{
Id = c.Int(nullable: false),Address1 = c.String(nullable: false,maxLength: 500),Address2 = c.String(maxLength: 500),City = c.String(nullable: false,maxLength: 100),State = c.String(nullable: false,maxLength: 10),Zip = c.String(nullable: false,maxLength: 5),CreatedDate = c.DateTime(nullable: false),ModifiedDate = c.DateTime(nullable: false),OrganizationsId = c.Int(),NotesId = c.Int(),CreatedByUserId = c.Int(),ModifiedByUserId = c.Int(),})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Organizations",t => t.OrganizationsId)
.ForeignKey("dbo.Notes",t => t.NotesId)
.ForeignKey("dbo.Customers",t => t.Id,cascadeDelete: true)
.ForeignKey("dbo.Users",t => t.CreatedByUserId)
.ForeignKey("dbo.Users",t => t.ModifiedByUserId)
.Index(t => t.Id)
.Index(t => t.OrganizationsId,name: "IX_Organizations_Id")
.Index(t => t.NotesId,name: "IX_Notes_Id")
.Index(t => t.CreatedByUserId,name: "IX_CreatedByUser_Id")
.Index(t => t.ModifiedByUserId,name: "IX_ModifiedByUser_Id");
但是,如果我添加第二个键,则不会。
这是使用复合键更改的新地址
public class Addresses : AuditBase
{
[Key,Column(Order = 1)]
public int Id { get; set; }
[Key,Column(Order = 2)]
public int EntityTypeId { get; set; }
使用外键参考和订单更新了客户类别
public class Customers
{
[Key,ForeignKey("Address")]
public int Id { get; set; }
[ForeignKey("Address"),Column(Order = 2)]
public int EntityTypeId { get; set; }
但是正如您所看到的,迁移现在包括对主体的显式引用,而不是复合键绑定[请参阅=>(CustomersId = c.Int(nullable:false)]
CreateTable(
"dbo.Addresses",EntityTypeId = c.Int(nullable: false),CustomersId = c.Int(nullable: false),})
.PrimaryKey(t => new { t.Id,t.EntityTypeId })
.ForeignKey("dbo.Organizations",t => t.Id)
.ForeignKey("dbo.Notes",t => t.Id)
.ForeignKey("dbo.Customers",t => t.CustomersId,t => t.ModifiedByUserId)
.Index(t => t.Id)
.Index(t => t.CustomersId,name: "IX_Customers_Id")
.Index(t => t.CreatedByUserId,name: "IX_ModifiedByUser_Id");
我尝试使用流畅的API进行变体,但是我一定做得不对。这有可能吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)