C# 复合密钥实体,不想声明PK密钥
这应该很简单。我有一节课C# 复合密钥实体,不想声明PK密钥,c#,entity-framework,entity-framework-6,ef-code-first,C#,Entity Framework,Entity Framework 6,Ef Code First,这应该很简单。我有一节课 public class ProductConfig { public Category { get;set; } public Product { get;set; } } 这两个导航属性也是表的主键。 声明PRoductId和CategoryId是冗余的。如何使用nav属性配置主键 编辑:愚蠢的我。我在上面的问题中忘记了一些非常重要的事情。上面这两个是为了指出配置。然后我们有第三个fk,它是为产品和类别组合选择的配置。所以上述实体必须是一个物化实体 p
public class ProductConfig
{
public Category { get;set; }
public Product { get;set; }
}
这两个导航属性也是表的主键。
声明PRoductId和CategoryId是冗余的。如何使用nav属性配置主键
编辑:愚蠢的我。我在上面的问题中忘记了一些非常重要的事情。上面这两个是为了指出配置。然后我们有第三个fk,它是为产品和类别组合选择的配置。所以上述实体必须是一个物化实体
public class ProductConfig
{
public Category { get;set; }
public Product { get;set; }
public ProductCategoryType { get; set; }
}
您只需通过导航属性设置产品和类别实体之间的关系。EF将通过自己的多对多关系设置正确的表结构。因此,不需要自己的关系实体。 请查看以下内容: e、 g: 产品类别:
public class Product
{
// other properties
public virtual ICollection<Category> Categories { get; set; }
}
有关更多信息,请阅读以下内容:
编辑2(获取信息后,需要EF<6.2的解决方案):
在您最后一个问题编辑之后,需要另一种解决方法。
我们走吧
您需要一个如下所示的结构:
产品
public class Product
{
// other properties
public virtual ICollection<ProductConfig> ProductConfigs { get; set; }
}
要在EF<6.2中设置唯一约束,您必须这样做:
modelBuilder.Entity<ProductConfig>()
.Property(e => e.Category)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute("YourIndex", 1) { IsUnique = true }));
modelBuilder.Entity<ProductConfig>()
.Property(e => e.Product)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute("YourIndex", 2) { IsUnique = true }));
modelBuilder.Entity<ProductConfig>()
.Property(e => e.ProductCategoryType)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute("YourIndex", 3) { IsUnique = true }));
modelBuilder.Entity()
.Property(e=>e.Category)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
新索引注释(新索引属性(“YourIndex”,1){IsUnique=true});
modelBuilder.Entity()
.Property(e=>e.Product)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
新索引注释(新索引属性(“YourIndex”,2){IsUnique=true});
modelBuilder.Entity()
.Property(e=>e.ProductCategoryType)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
新索引注释(新索引属性(“YourIndex”,3){IsUnique=true});
在EF 6.2中:
modelBuilder.Entity<Person>()
.HasIndex(p => new { p.Category, p.Product, p.ProductCategoryType })
.IsUnique();
modelBuilder.Entity()
.HasIndex(p=>new{p.Category,p.Product,p.ProductCategoryType})
.IsUnique();
编辑3
如果ProductConfig类中没有主键,或者在我添加了主键的示例中使用了我的主键,因为我认为您已经拥有了该类。
可以将多个属性设置为键。这也将导致独特的组合。
您可以使用以下内容(而不是索引内容)对其进行归档:
modelBuilder.Entity<ProductConfig>()
.HasKey(pc => new { pc.Category, pc.Product, pc.ProductCategoryType });
modelBuilder.Entity()
.HasKey(pc=>新{pc.类别,pc.产品,pc.ProductCategoryType});
有关更多信息,请查看
您还可以添加一个Id作为主键,而不需要索引
声明ProductId和CategoryId是多余的。如何使用nav属性配置主键
很快-你不能。虽然EF6支持基于卷影属性的FKs,但它不提供使用卷影属性名称配置PK(以及许多其他与列相关的设置)的方法-[Key]
,[column]
数据注释无法应用于导航属性,并且HasKey
fluent API需要原始属性选择器表达式。通常,EF6不支持PK中的阴影属性
所有这些限制都已在EF Core中消除。但在EF6中,无论是否冗余,您必须在实体中定义实际的原语属性,并将它们映射到复合PK。我们希望将复合表作为实体,以便查询它。但现在我想起来,也许我们不需要这个。因为我们总是使用一个类别或一个产品作为起点。让我试试..哎呀,我忘了我问题中一件很重要的事。Im editign nowWell,我已经添加了一些更多的信息来存档具有附加属性或更多属性的复合表的目标。嗯,我们使用EntityTypeConfiguration类,它没有HasIndex方法。我想知道我是否需要升级EF。或者在使用EntityTypeConfiguration时它被命名为Differentitlyl?这似乎是6.2的特性,我们使用的是6.1.3。好吧,听了很难过。我添加了PKs和标准HasRequired(…).WithMany().HasForeignKey(…);哪个有效
public class ProductConfig
{
// other properties
public virtual Category { get; set; }
public virtual Product { get; set; }
public virtual ProductCategoryType { get; set; }
}
modelBuilder.Entity<ProductConfig>()
.Property(e => e.Category)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute("YourIndex", 1) { IsUnique = true }));
modelBuilder.Entity<ProductConfig>()
.Property(e => e.Product)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute("YourIndex", 2) { IsUnique = true }));
modelBuilder.Entity<ProductConfig>()
.Property(e => e.ProductCategoryType)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute("YourIndex", 3) { IsUnique = true }));
modelBuilder.Entity<Person>()
.HasIndex(p => new { p.Category, p.Product, p.ProductCategoryType })
.IsUnique();
modelBuilder.Entity<ProductConfig>()
.HasKey(pc => new { pc.Category, pc.Product, pc.ProductCategoryType });