模型生成器再添加一列?
model builder add one column more?
我先用EF代码。
这是我的模型构建器
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Market>()
.HasRequired(s => s.State)
.WithMany()
.HasForeignKey(s => s.StateId)
.WillCascadeOnDelete(false);
}
和状态 Class :
public class State
{
public State()
{
Markets = new HashSet<Market>();
}
[Key]
public int StateId { get; set; }
public string StateName { get; set; }
// navigation property
public virtual ICollection<Market> Markets { get; set; }
}
和市场class:
public class Market
{
[Key]
public int MarketId { get; set; }
public string MarketName { get; set; }
public int StateId { get; set; }
// navigation property
public virtual State State { get; set; }
}
当然我删除了额外的代码。
问题是当我使用此代码时,State_StateId 列添加到我的市场 table 数据库中,当我不使用模型构建器时,消息循环代码发生错误并且... (我说我删除了额外的代码),那么如果没有这个 "State_StateId" 额外的列,我怎么能先使用代码。
不好意思英文写的不好
如果要删除 State_StateId
列,请完全按照下面的代码设置配置,不要让 WithMany
为空:
modelBuilder.Entity<Market>()
.HasRequired(s => s.State)
.WithMany(p => p.Markets)
.HasForeignKey(s => s.StateId)
.WillCascadeOnDelete(false);
或者您可以删除 Fluent API 配置,让 EF 使用默认配置约定,并为您设置所有表、主键、外键和列名。
我先用EF代码。
这是我的模型构建器
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Market>()
.HasRequired(s => s.State)
.WithMany()
.HasForeignKey(s => s.StateId)
.WillCascadeOnDelete(false);
}
和状态 Class :
public class State
{
public State()
{
Markets = new HashSet<Market>();
}
[Key]
public int StateId { get; set; }
public string StateName { get; set; }
// navigation property
public virtual ICollection<Market> Markets { get; set; }
}
和市场class:
public class Market
{
[Key]
public int MarketId { get; set; }
public string MarketName { get; set; }
public int StateId { get; set; }
// navigation property
public virtual State State { get; set; }
}
当然我删除了额外的代码。
问题是当我使用此代码时,State_StateId 列添加到我的市场 table 数据库中,当我不使用模型构建器时,消息循环代码发生错误并且... (我说我删除了额外的代码),那么如果没有这个 "State_StateId" 额外的列,我怎么能先使用代码。
不好意思英文写的不好
如果要删除 State_StateId
列,请完全按照下面的代码设置配置,不要让 WithMany
为空:
modelBuilder.Entity<Market>()
.HasRequired(s => s.State)
.WithMany(p => p.Markets)
.HasForeignKey(s => s.StateId)
.WillCascadeOnDelete(false);
或者您可以删除 Fluent API 配置,让 EF 使用默认配置约定,并为您设置所有表、主键、外键和列名。