0

我有一个首先使用代码的 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 感到沮丧。我已经走了很远,但开始感到气馁。

4

1 回答 1

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);
 }
于 2013-10-30T07:24:13.137 回答