2014-09-18 126 views
3

使用EF6,我創建了一個簡單的POCO指定帳戶跟蹤有關用戶的數據:EF創建額外的表

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

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

這個客戶對象是以下的DbContext的分開(剝離爲了簡潔):

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

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

OnModelCreating刪除了PluralTableNamesConvention,因此它成功地創建了一個名爲「Account」的表,並具有正確的屬性。

我也有ApplicationDbContext,其中包含ApplicationUser定義如下:

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

    public virtual Account AccountInfo { get; set; } 
} 

的目的是爲了能夠創建一個用戶,然後撥打電話如

myStuff.SearchByName(user.AccountInfo.FirstName) 

問題: 當我創建一個用戶並且EF生成ASP.net標識表時,它也創建了一個名爲「Accounts」的表。所有創建的用戶都將其信息發送到「帳戶」表中,而不是我打算使用的原始「帳戶」表。

我不知道如何強制IdentityDbContext只使用原來的帳戶表,並不生成自己的。我曾嘗試過:

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

但它返回以下內容。任何幫助將不勝感激:)

模型兼容性無法檢查,因爲數據庫不包含模型元數據。只能使用Code First或Code First Migrations創建的數據庫檢查模型兼容性。

+0

是否啓用了在項目遷移? http://msdn.microsoft.com/en-us/data/jj591621.aspx – 2014-09-25 11:05:09

+0

@ Praval'Shaun'Tirubeni是的,項目中啓用了遷移。 – 2014-09-29 15:38:47

回答

0

好吧,所以不知道這是否會成爲一個有效的答案。但是前段時間我創建了一個MVC4/EF5項目,並且不得不做類似的事情。我使用WebSecurity Identity Framework或任何它所稱的(認爲現在有一個不同的身份框架)。基本上,我所要做的只是在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); 
} 

所以我只是告訴它使用「芭蕾舞表」來存儲信息。您可能會使用不同的身份框架,因爲您使用的是EF6,但您應該可以在API中找到類似的東西。

My Github project if you want to look through the code.