简体   繁体   English

EF创建额外表

[英]EF Creating Extra Table

Using EF6, I have created a simple POCO named Account to track data about the user: 使用EF6,我创建了一个名为Account的简单POCO来跟踪用户数据:

public class Account
{
    public int ID { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    //Others
}

This Account object is apart of the following DbContext (stripped for brevity): 此Account对象是以下DbContext的一部分(为简洁而剥离):

public class MyContext : DbContext
{
    //constructor
    //other DBSets

    public DbSet<Account> Accounts {get;set;}
}

The OnModelCreating removes the PluralTableNamesConvention, so it successfully creates a table named "Account" with the correct properties. OnModelCreating删除了PluralTableNamesConvention,因此它成功创建了一个名为“Account”的表,其中包含正确的属性。

I also have the ApplicationDbContext, which contains the following definition for ApplicationUser: 我还有ApplicationDbContext,它包含ApplicationUser的以下定义:

public class ApplicationUser : IdentityUser
{
    //GenerateUserIdentityAsync method....

    public virtual Account AccountInfo { get; set; }
}

The intent is to be able to create a user, then make calls such as 目的是能够创建一个用户,然后进行如下调用

myStuff.SearchByName(user.AccountInfo.FirstName)

PROBLEM: When I create a user and EF generates the ASP.net identity tables, it ALSO creates a table named "Accounts". 问题:当我创建用户并且EF生成ASP.net身份表时,它还会创建一个名为“Accounts”的表。 All users created have their information sent into the "Accounts" table, not the original "Account" table that I intended to use. 创建的所有用户都将其信息发送到“帐户”表,而不是我打算使用的原始“帐户”表。

I am not sure how force the IdentityDbContext to use only the original Account table and not generate its own. 我不确定IdentityDbContext如何强制仅使用原始Account表而不生成自己的。 I have tried: 我努力了:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Account>().ToTable("Account");
    base.OnModelCreating(modelBuilder);
}

but it returns the following. 但它返回以下内容。 Any help would be appreciated :) 任何帮助,将不胜感激 :)

Model compatibility cannot be checked because the database does not contain model metadata. 无法检查模型兼容性,因为数据库不包含模型元数据。 Model compatibility can only be checked for databases created using Code First or Code First Migrations. 只能检查使用Code First或Code First Migrations创建的数据库的模型兼容性。

Okay so not sure if this is going to be a valid answer or not. 好的,所以不确定这是否是一个有效的答案。 But I created a MVC4/EF5 project a while ago and had to do something similar. 但是我不久前创建了一个MVC4 / EF5项目并且必须做类似的事情。 I used the WebSecurity Identity Framework or whatever it's called (Think there is a different identity framework now). 我使用了WebSecurity Identity Framework或其所谓的(认为现在有一个不同的身份框架)。 Essentially all I had to do was put a few lines of code in the Application_Start method of the Global.ascx. 基本上我所要做的就是在Global.ascx的Application_Start方法中添加几行代码。

// Tells membership provider to use the Baller table too store user profile information
if (!WebSecurity.Initialized)
{
    WebSecurity.InitializeDatabaseConnection("BallSoHardContext", "Baller", "Id", "UserName", autoCreateTables: false);
}

So I just tell it to use the "Baller table" to store the information. 所以我只是告诉它使用“Baller table”来存储信息。 You might be using a different identity framework since you are using EF6, but you should be able to find something similar in the API. 您可能正在使用不同的身份框架,因为您使用的是EF6,但您应该能够在API中找到类似的东西。

My Github project if you want to look through the code. 我的Github项目,如果你想查看代码。

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

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