【发布时间】:2018-05-03 09:52:51
【问题描述】:
我有一个DBContext 代码优先方法。它将接受连接字符串作为参数。
可以根据用户提供的连接字符串忽略外键约束吗?
public class EFDbContext : DbContext{
public EFDbContext(string connection="Default"):base(connection)
{
}
public DbSet<Contact> Contact { get; set; }
}
我的Contact 类如下所示:
public class Contact
{
[Key]
public long ContactId { get; set; }
//Foreign key to Contact
[ForeignKey("SystemUsers")] //**Need to ignore this constaint If DB is NoRelationsDB**
public Guid UserId { get; set; }
public virtual SystemUsers SystemUsers { get; set; }
}
我对实体的用法是:
EFDbContext context=new EFDbContext();
EFDbContext context1=new EFDbContext("NoRelationsDB"); //If the connection string is this then we have to ignore all the constraints in Tables.
【问题讨论】:
-
如果你想忽略约束,拥有它们有什么意义?约束是有原因的。您有时想忽略它们的原因是什么?不知何故,它闻起来像是设计缺陷。
标签: asp.net-mvc entity-framework connection-string