C# 是否先将外键作为EF代码中的主键?
我需要映射域中3个域模型之间的关系,其中一个域模型是关系模型的聚合根C# 是否先将外键作为EF代码中的主键?,c#,sql,entity-framework,ef-code-first,domain-driven-design,C#,Sql,Entity Framework,Ef Code First,Domain Driven Design,我需要映射域中3个域模型之间的关系,其中一个域模型是关系模型的聚合根 public class Entity1 { public int Id { get; set; } } public class Entity2 { public int Id { get; set; } } public class SuperEntity { public int Id { get; set; } // bounded context for relationship
public class Entity1 {
public int Id { get; set; }
}
public class Entity2 {
public int Id { get; set; }
}
public class SuperEntity {
public int Id { get; set; }
// bounded context for relationship classes
}
关系实体应该如下所示
public class Relationship {
public int RelationshipId { get; set; }
public Entity1 Entity1 { get; set; }
public Entity2 Entity2 { get; set; }
}
接下来,超级实体的外观应该如下所示:
public class SuperEntity {
public int Id { get; set; }
public ICollection<Relationship> Relationships { get; set; }
}
Table_Relationships
[ SuperEntity_Id // Foreign-key to SuperEntity
PrimaryKey [ Entity1_Id // Foreign-key to Entity1
[ Entity2_Id // Foreign-key to Entity2
这意味着表\u关系的主键将是SuperEntity\u Id+Entity1\u Id+Entity2\u Id
是否可以先将其映射到EF代码中?为什么不使用DataAnnotations.KeyAttribute?这是在域模型类中定义复杂主键的明确方法