我有一个首先使用代码的 MVC4 应用程序。我创建了 3 个模型,我希望使用这些模型来跟踪我的客户 Associates,每个模型都有一个 AssociateType(分销商或零售商)的名称,每个 Associate 也有一个指定的区域(北佛罗里达或南佛罗里达。
我首先创建以下模型并运行 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; }
}
}
'区域.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; }
}
}
数据库上下文
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 & AssociateType 表。
地区:(北佛罗里达和南佛罗里达) AssociateTypes(分销商和零售商)
这样,当我在 CRUD 操作期间添加员工时,我将有两个下拉菜单,其中包含 AssociateType 的选项(分销商和零售商)和该员工地区的(N 或 S Florida)。
任何帮助将不胜感激。我真的对 MVC 感到沮丧。我已经走了很远,但开始感到气馁。