Question

I have an MVC4 application that is using code first. I have created 3 models that I wish to use to keep track of my clients Associates each with a designation of AssociateType (Distributors or Retailers) each Associate also has a deignated Region (North Florida or South Florida.

I started off by creating the following models and running update-database to create the tables in the 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);
            }
        }
    }

My Tables are not being created with the IDENTITY SPECIFICATION (Is Identity) being set to Yes..... WHY IS THIS?? Because of this I cannot add any data to the tables or I get an error:

Cannot insert the value NULL into column 'RegionID', table 'XXXDb.dbo.Regions'; column does not allow nulls. INSERT FAILS.

My goal is to populate the Region & AssociateType tables with just a few rows of items.

Regions: (North Florida & South Florida) AssociateTypes (Distributors & Retailers)

This way when I add an Associate during a CRUD operation I would have two drop downs that have the options (Distributors & Retailers) for AssociateType and (N or S Florida) for that associates Region.

Any help would be very much appreciated. I'm really getting frustrated with MVC. I have made it pretty far, but starting to get discouraged.

Was it helpful?

Solution

I did some test and here is the solution that work on my machine I just kept the navigation properties of your obejcts.

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; }
}

Then in the OnModelCreating you have to add the following two commands and this should generate the database that you want

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);

and in the class constructor you may add this code

 public XXXDb(): base("name=DefaultConnection")
 { 
     Database.SetInitializer(new DropCreateDatabaseIfModelChanges<XXXDb>());
     Database.Initialize(force: true);
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top