我有一个首先使用代码的MVC4应用程序。我已经创建了3个模型,我希望用它来跟踪我的客户。每个模型都指定了AssociateType(分销商或零售商)每个员工也有一个被拒绝的地区(北佛罗里达州或南佛罗里达州。
我开始创建以下模型并运行update-database以在数据库中创建表。
Associate.cs
namespace XXX.Models
{
public class Associate
{
public int AssociateID { get; set; }
[StringLength(50), Column(TypeName = "varchar")]
public string AssociateName { get; set; }
public int AddressNumber { get; set; }
[StringLength(50), Column(TypeName = "varchar")]
public string AddressStreet { get; set; }
[StringLength(20), Column(TypeName = "varchar")]
public string AddressCity { get; set; }
[StringLength(2), Column(TypeName = "varchar")]
public string State { get; set; }
[StringLength(10), Column(TypeName = "varchar")]
public string Zipcode { get; set; }
[StringLength(16), Column(TypeName = "varchar")]
public string MainPhoneNumber { get; set; }
[StringLength(60), Column(TypeName = "varchar")]
public string AssociateEmail { get; set; }
[StringLength(80), Column(TypeName = "varchar")]
public string AssociateWebsite { get; set; }
//See Corresponding Navigation Properties
[Display(Name = "Region")]
public int RegionID { get; set; }
[Display(Name = "AssociateType")]
public int AssociateTypeID { get; set; }
[StringLength(35), Column(TypeName = "varchar")]
public string ContactFirstName { get; set; }
[StringLength(35), Column(TypeName = "varchar")]
public string ContactLastName { get; set; }
[StringLength(16), Column(TypeName = "varchar")]
public string ContactPhoneNumber { get; set; }
[StringLength(60), Column(TypeName = "varchar")]
public string ContactEmail { get; set; }
public virtual Region Region { get; set; }
public virtual AssociateType AssociateType { get; set; }
}
AssociateType.cs
namespace XXX.Models
{
public class AssociateType
{
[ForeignKey("Associate")]
public int AssociateTypeID { get; set; }
[StringLength(50), Column(TypeName = "varchar")]
public string AssociateTypeName { get; set; }
public virtual Associate Associate { get; set; }
}
}
'Region.cs'
namespace XXX.Models
{
public class Region
{
public int RegionID { get; set; }
[StringLength(20), Column(TypeName = "varchar")]
public string RegionName { get; set; }
[Column(TypeName = "varchar(Max)")]
public string RegionDescription { get; set; }
public virtual Associate Associate { get; set; }
}
}
的DbContext
namespace XXX.Models
{
public class XXXDb : DbContext
{
public XXXDb(): base("name=DefaultConnection")
{
}
public DbSet<Associate> Associates { get; set; }
public DbSet<AssociateType> AssociateType { get; set; }
public DbSet<Ingredient> Ingredients { get; set; }
public DbSet<Region> Regions { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Associate>().HasKey(a => a.AssociateID);
modelBuilder.Entity<Associate>().Property(a => a.AssociateID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Associate>().HasRequired(at => at.AssociateType)
.WithRequiredDependent();
modelBuilder.Entity<Associate>().HasRequired(r => r.Region)
.WithRequiredDependent();
modelBuilder.Entity<AssociateType>().HasKey(at => at.AssociateTypeID);
modelBuilder.Entity<AssociateType>().Property(at => at.AssociateTypeID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Region>().HasKey(r => r.RegionID);
modelBuilder.Entity<Region>().Property(r => r.RegionID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
}
我的桌子没有被创建,IDENTITY SPECIFICATION(Is Identity)被设置为Yes .....为什么?因此,我无法向表中添加任何数据,或者出现错误:
Cannot insert the value NULL into column 'RegionID', table 'XXXDb.dbo.Regions'; column does not allow nulls. INSERT FAILS.
我的目标是填充Region&amp; AssociateType表只有几行项目。
地区:(北佛罗里达州和佛罗里达州南部) AssociateTypes(分销商和零售商)
这样当我在CRUD操作期间添加一个Associate时,我会有两个下拉列表,其中包含AssociateType和(N或S Florida)的选项(Distributors&amp; Retailers)。
非常感谢任何帮助。我真的对MVC感到沮丧。我已经做了很多,但开始气馁。
答案 0 :(得分:2)
我做了一些测试,这是在我的机器上运行的解决方案我只保留了你的对象的导航属性。
public class Associate
{
public int AssociateID { get; set; }
public int RegionID { get; set; }
public virtual Region Region { get; set; }
public int AssociateTypeID { get; set; }
public virtual AssociateType AssociateType { get; set; }
}
public class Region
{
public int RegionID { get; set; }
[StringLength(50), Column(TypeName = "varchar")]
public string IngredientNameEn { get; set; }
[Column(TypeName = "varchar(Max)")]
public string IngredientNameEs { get; set; }
public virtual List<Associate> Associates { get; set; }
}
public class AssociateType
{
public int AssociateTypeID { get; set; }
[StringLength(50), Column(TypeName = "varchar")]
public string AssociateTypeName { get; set; }
public virtual List<Associate> Associates { get; set; }
}
然后在OnModelCreating中你必须添加以下两个命令,这应该生成你想要的数据库
modelBuilder.Entity<Region>().HasMany(a => a.Associates)
.WithRequired(r => r.Region).HasForeignKey(r => r.RegionID);
modelBuilder.Entity<AssociateType>().HasMany(a => a.Associates)
.WithRequired(r => r.AssociateType).HasForeignKey(r => r.AssociateTypeID);
在类构造函数中,您可以添加此代码
public XXXDb(): base("name=DefaultConnection")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<XXXDb>());
Database.Initialize(force: true);
}