重命名外键列不流畅 api
Rename foreign key column not working in fluent api
我正在尝试进行自我引用 table
public class Category
{
// PK
public int CategoryId { get; set; }
// Property
public string CategoryName { get; set; }
// FK
public int? ParentCategoryId { get; set; }
public virtual ICollection<Category> ParentCategories { get; set; }
public virtual ICollection<Product> Products { get; set; } // Product is defined later
}
和配置:
public class CategoryConfiguration : EntityTypeConfiguration<Category>
{
public CategoryConfiguration():base()
{
HasKey(c => new { c.CategoryId });
HasOptional(c => c.ParentCategories)
.WithMany()
.HasForeignKey(c => c.ParentCategoryId );
}
}
想法是使用 ParentCategoryId
作为列名,但它不起作用。相反,它生成了一个名为:Category_CategoryId
.
的列
我试过.Map(c => c.MapKey("ParentCategoryId"))
,结果还是一样。
我不认为这是自引用的原因,因为在多对多关系中也会发生同样的事情:
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
和Product
配置
public class ProductConfiguration:EntityTypeConfiguration<Product>
{
public ProductConfiguration():base()
{
// Many-to-Many
HasMany(c => c.Categories)
.WithMany()
.Map(p =>
{
p.MapLeftKey("ProductRefId");
p.MapRightKey("CategoryRefId");
p.ToTable("ProductCategory");
});
}
}
table 名称是 ProductCategories
而不是 ProductCategory
外键是 Product_ProductId
和 Category_CategoryId
他们都不如所料。
我该如何解决这个问题?请帮助。
谢谢!
更新 1
奇怪的是,如果我通过 DbModelBuilder
定义它,那么它就可以工作了
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>()
.HasOptional(c => c.ParentCategories)
.WithMany()
.HasForeignKey(c => c.ParentCategoryId);
}
外键如预期变为ParentCategoryId
。
问题解决了,我弄错了。我没有将配置挂接到 DbContext。
通过将这些添加到 DbContext
,列按预期重命名。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new CategoryConfiguration());
modelBuilder.Configurations.Add(new ProductConfiguration());
}
我正在尝试进行自我引用 table
public class Category
{
// PK
public int CategoryId { get; set; }
// Property
public string CategoryName { get; set; }
// FK
public int? ParentCategoryId { get; set; }
public virtual ICollection<Category> ParentCategories { get; set; }
public virtual ICollection<Product> Products { get; set; } // Product is defined later
}
和配置:
public class CategoryConfiguration : EntityTypeConfiguration<Category>
{
public CategoryConfiguration():base()
{
HasKey(c => new { c.CategoryId });
HasOptional(c => c.ParentCategories)
.WithMany()
.HasForeignKey(c => c.ParentCategoryId );
}
}
想法是使用 ParentCategoryId
作为列名,但它不起作用。相反,它生成了一个名为:Category_CategoryId
.
我试过.Map(c => c.MapKey("ParentCategoryId"))
,结果还是一样。
我不认为这是自引用的原因,因为在多对多关系中也会发生同样的事情:
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
和Product
配置
public class ProductConfiguration:EntityTypeConfiguration<Product>
{
public ProductConfiguration():base()
{
// Many-to-Many
HasMany(c => c.Categories)
.WithMany()
.Map(p =>
{
p.MapLeftKey("ProductRefId");
p.MapRightKey("CategoryRefId");
p.ToTable("ProductCategory");
});
}
}
table 名称是 ProductCategories
而不是 ProductCategory
外键是 Product_ProductId
和 Category_CategoryId
他们都不如所料。
我该如何解决这个问题?请帮助。
谢谢!
更新 1
奇怪的是,如果我通过 DbModelBuilder
定义它,那么它就可以工作了
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>()
.HasOptional(c => c.ParentCategories)
.WithMany()
.HasForeignKey(c => c.ParentCategoryId);
}
外键如预期变为ParentCategoryId
。
问题解决了,我弄错了。我没有将配置挂接到 DbContext。
通过将这些添加到 DbContext
,列按预期重命名。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new CategoryConfiguration());
modelBuilder.Configurations.Add(new ProductConfiguration());
}