简体   繁体   English

在AccountContext中添加新属性,EF Code First?

[英]Adding new properties in AccountContext, EF Code First?

In a POC application I have started with default MVC-4 internet application template. 在POC应用程序中,我已经开始使用默认的MVC-4互联网应用程序模板。 I have added few more properties in AccountContext : 我在AccountContext添加了更多属性:

public class AccountsContext : DbContext
{
    public AccountsContext()
        : base("vs12localdb")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Employee> Employees{ get; set; }
    public DbSet<Work> DailyWorks { get; set; }
}

But when trying to add Employee entity in the HttpPost method : 但是当尝试在HttpPost方法中添加Employee实体时:

    [HttpPost]
    public ActionResult Create(EmployeeViewModel emp)
    {
        if (ModelState.IsValid)
        {
            emp.UserId = WebSecurity.HasUserId ? WebSecurity.CurrentUserId : -1;
            db.Employees.Add(emp);
            db.SaveChanges();  //Causing Error: Invalid object name 'dbo.Employees'.
            return RedirectToAction("Index");
        }

        return View(emp);
    }

I see entity framework ignoring to create Employee & Work entities in the database. 我看到实体框架忽略了在数据库中创建Employee&Work实体。 在此输入图像描述

Thus getting following error, while doing db.SaveChanges(); 因此在执行db.SaveChanges();出现以下错误db.SaveChanges(); :

Invalid object name 'dbo.Employees' 无效的对象名称'dbo.Employees'

What could be the issue ? 可能是什么问题 ?

You can set Entity Framework to the following: 您可以将Entity Framework设置为以下内容:

CreateDatabaseIfNotExists<TContext>
DropCreateDatabaseAlways<TContext>

I personally use CreateDatabaseIfNotExists like so: 我个人使用CreateDatabaseIfNotExists,如下所示:

public class ContextInitializer : CreateDatabaseIfNotExists<Context>
{
    protected override void Seed(Context ctx)
    {
         // any seed data

    } 

}

Just make sure you Initilizer the Membership after this takes place, somewhere in Global.asax or a static function called there: 在发生这种情况之后,确保Initilizer成员身份,在Global.asax中的某处或在那里调用的静态函数:

I generally use something like this: 我通常使用这样的东西:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    AuthConfig.RegisterAuth();
    AutoMapperConfig.RegisterConfig();

    Database.SetInitializer(new ContextInitializer());

    using (var context = new Context())
    {
        context.Database.Initialize(false);
    }

    if (!WebSecurity.Initialized)
    {
        WebSecurity.InitializeDatabaseConnection(
            connectionStringName: "DefaultConnection", 
            userTableName: "User", 
            userIdColumn: "Id", 
            userNameColumn: "Email", 
            autoCreateTables: false);
    }
}

If you plan on continuously adding, removing properties from Entities and adding new ones, you may want to look up Code First migrations which can handle updating the Database for you. 如果您计划不断添加,删除实体中的属性并添加新属性,您可能需要查找可以为您更新数据库的Code First迁移。

Click here for more ~ Code First Migrations 点击这里了解更多〜Code First Migrations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM