2012-03-18 52 views
1

我想添加一個外鍵引用的aspnet_users表到我的用戶表,以簡單地擴展aspnet_users。我正在使用EF,我不想創建一個aspnet_users實體類。有沒有辦法使用流利的API來創建EF目前未跟蹤的表的外鍵引用?實體框架設置外鍵引用aspnet_users表W/O實體類

我知道這並不流利的API,但我想以下w /失敗顯而易見:

[ForeignKey("aspnet_users.UserId")] 

感謝。

+0

有[關於這個問題的大量問題(http://stackoverflow.com/questions/6514733/entity-framework-code-first-aspnet-users-mapping-joins)和他們都說:不要映射到aspnet_users](http://stackoverflow.com/questions/6514733/entity-framework-code-first-aspnet-users-mapping-joins)。 – 2012-03-18 14:52:47

+0

爲了清楚起見,你的意思是不要設置外鍵引用它? – 2012-03-18 17:54:50

+0

嗯,而是:[不要映射。](http://stackoverflow.com/a/6521227/861716) – 2012-03-18 17:58:07

回答

1

這對流利的API來說也是不可能的。您將不得不手動執行SQL來創建FK。如果你正在使用數據庫初始化程序,那麼你可以嘗試這樣的事情。

public class MyInitializer : CreateDatabaseIfNotExists<MyDbContext> 
{ 
    protected override void Seed(MyDbContext context) 
    { 
     //your logic here 
     context.Database.ExecuteSqlCommand("alter table MyTable 
add constraint MyTable_MyColumn_FK FOREIGN KEY (MyColumn) references aspnet_users(UserId)");  
    } 
} 
+0

謝謝,假設情況會是這樣的。 – 2012-03-18 18:08:04