1
我首先使用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);
}
和國家A級:
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; }
}
和市場類:
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列添加到我的Market數據庫表中,並且當我不使用modelbuilder時,消息循環代碼發生錯誤...(我說我刪除了額外的代碼) ,所以如何在沒有這個「State_StateId」額外列的情況下先使用代碼。
請原諒我英文寫作不好。
謝謝你的幫助,它很有用。 – user3087175