流利的API在SQL Server中没有创造身份规范(是一种身份) [英] Fluent API not creating Identity Specification (Is Identity) in SQL Server
问题描述
我有一个先用code的MVC4应用程序。我创建了3款车型,我想用它来保持我的客户跟踪将每个与AssociateType的指定(分销商或零售商)每一位员工也有一个deignated地区(北佛罗里达州或佛罗里达州南部。
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
'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
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规格(是标识)正在创建设置为Yes .....这是为什么?正因为如此,我不能添加任何数据表或我得到一个错误:
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:
无法插入NULL值插入列'RegionID',表'XXXDb.dbo.Regions';列不允许空值。插入失败。
我的目标是填充地区放大器; AssociateType表的项目只有几行。
My goal is to populate the Region & AssociateType tables with just a few rows of items.
地区(北佛罗里达和放大器;南佛罗里达)
AssociateTypes(分销商和放大器;零售商)
Regions: (North Florida & South Florida) AssociateTypes (Distributors & Retailers)
此方式,当我一个CRUD操作过程中添加的助理我必须拥有的选项(分销商和放大器;零售商)二下拉菜单为AssociateType和(N或S佛罗里达)为关联区域。
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.
任何帮助将非常AP preciated。我真的感到沮丧与MVC。我已pretty远,但开始变得气馁。
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.
推荐答案
我做了一些测试,下面是我的机器上工作,我只是不停的obejcts的导航属性的解决方案。
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; }
}
然后在OnModelCreating必须添加以下两个命令,这应该生成所需的数据库
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);
和在类的构造您可以添加此code
and in the class constructor you may add this code
public XXXDb(): base("name=DefaultConnection")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<XXXDb>());
Database.Initialize(force: true);
}
这篇关于流利的API在SQL Server中没有创造身份规范(是一种身份)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!