您好我正在使用Fluent API的Entity框架在DB中创建表。当我创建我的第一个项目时它工作正常.Table已经在提到的数据库中创建。当尝试创建第二个项目时它显示错误
支持' EmployeeDetailsContext'自创建数据库以来,上下文已更改。请考虑使用代码优先迁移来更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
我想在db方面创建表格我的值(EmployeeDetails.cs)
EmployeeDetails.cs是
public class EmployeeDetails
{
public int EmployeeId { get; set; }
//[StringLength(20)]
public string FirstName { get; set; }
//[StringLength(20)]
public string LastName { get; set; }
//[MaxLength(3)]
public int EmpAge { get; set; }
//[StringLength(100)]
public string Address { get; set; }
public int MobileNO { get; set; }
public int EmpRankId { get; set; }
public string JobTitle { get; set; }
}
EmployeeDetailsContext.cs
public class EmployeeDetailsContext:DbContext
{
public DbSet<EmployeeDetails> employeedet { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// The primary Key for the EmployeeId
modelBuilder.Entity<EmployeeDetails>().HasKey(t => t.EmployeeId);
// The Identity Key for the EmployeeId
modelBuilder.Entity<EmployeeDetails>().Property(c => c.EmployeeId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
//max length for FirstName
modelBuilder.Entity<EmployeeDetails>()
.Property(d => d.FirstName).IsRequired()
.HasMaxLength(20);
//max length for LastName
modelBuilder.Entity<EmployeeDetails>()
.Property(d => d.LastName).IsRequired()
.HasMaxLength(20);
modelBuilder.Entity<EmployeeDetails>()
.Property(d => d.EmpAge).IsOptional();
modelBuilder.Entity<EmployeeDetails>()
.Property(d => d.Address).IsOptional();
//modelBuilder.Entity<Employee>()
// .Property(d => d.MobileNO).IsRequired();
modelBuilder.Entity<EmployeeDetails>()
.Property(d => d.EmpRankId).IsRequired();
modelBuilder.Entity<EmployeeDetails>()
.Property(d => d.JobTitle).IsOptional();
}
}
连接字符串是
<connectionStrings>
<add name="EmployeeDetailsContext" connectionString="Data Source=inchser001\inchdb002; database=Test1;Persist Security Info=True;User ID=sa;Password=*******" providerName="System.Data.SqlClient" />
</connectionStrings>
当我创建我的第一个项目时它运行正常.Table已经在提到的数据库中创建。当尝试创建第二个项目时它显示错误
支持&#39; EmployeeDetailsContext&#39;自创建数据库以来,上下文已更改。考虑使用Code First Migrations来更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
我想在db方面创建表格我的值(EmployeeDetails.cs)
我尝试了一些sugg(https://stackoverflow.com/),但它不起作用......
提供一些想法,它将帮助我进一步推进
答案 0 :(得分:3)
您只需要将错误消息说明:使用Code First Migrations。