[英]EF5 Code First: Detect if it creates a database so I can run ALTER statements
I am new with Entity Framework 5. Our team is using Code First
workflow. 我是Entity Framework 5的新手。我们的团队正在使用Code First
工作流程。
Before I'll start with my main question, let me first show you what I have tried ( the ultimate comment of all time :D
) . 在开始我的主要问题之前, 让我首先向您展示我已经尝试过的内容 ( 所有时间的最终评语:D
) 。
public class MyDBContext : CDBContext
{
public MyDBContext() : base(connString) { }
public MyDBContext(string connStr) : base(connStr) { }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// removes some conventions
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
// ........
// model configurations which contains mappings
modelBuilder.Configurations.Add(new AccountConfiguration());
// ........
// calls base OnModelCreating
base.OnModelCreating(modelBuilder);
}
// list of all Entity
public DbSet<Account> Account { get; set; }
}
MyDBContext
is the class I have created that inherits from CBDContext
that contains override methods and which also inherits from DBContext
. MyDBContext
是我创建的类,它从CBDContext
继承, CBDContext
包含重写方法,并且也从DBContext
继承。 One of the problems I have encountered is that entity framework doesn't handle field uniqueness. 我遇到的问题之一是实体框架无法处理字段唯一性。 I have already read the article on Configuring/Mapping Properties and Types with the Fluent API on their site and I can't find any configuration to set a property into unique. 我已经在他们的站点上阅读了有关使用Fluent API配置/映射属性和类型的文章,但找不到用于将属性设置为唯一的任何配置。
So what I did in order to set the field unique is to manually run several ALTER sql statements during creation, 因此,为了将字段设置为唯一,我要做的是在创建过程中手动运行多个ALTER sql语句,
using (MyDBContext _context = new MyDBContext(connString))
{
if (_context.Database.CreateIfNotExists())
{
_context.Database.ExecuteSqlCommand("ALTER TABLE Account ADD CONSTRAINT UQ_Account_AccountNumber UNIQUE(AccountNumber)");
_context.Database.ExecuteSqlCommand("ALTER TABLE Account ADD CONSTRAINT UQ_Account_GUID UNIQUE(GUID)");
// .... more on this in the following lines ...
}
}
My Questions: 我的问题:
if (_context.Database.CreateIfNotExists())
somewhere to an available method that can be overriden? 有没有一种方法可以在运行时检测或知道EF是否创建数据库,因此if (_context.Database.CreateIfNotExists())
某个位置可以覆盖或覆盖,则可以移动或隐藏该语句? What I really want is to remove if (_context.Database.CreateIfNotExists())
from the using statemnt and put it somewhere else or inside MyDBContext
so my code will look like this, 我真正想要的是从使用statemnt中删除if (_context.Database.CreateIfNotExists())
,并将其放在其他位置或MyDBContext
这样我的代码将如下所示:
using (MyDBContext _context = new MyDBContext(connString))
{
Account _acc = new Account()
// ...Account properties ...
_context.Account.Add(_acc);
_context.SaveChanges();
}
Thanks. 谢谢。
You should take a look at Code First Migrations , more specific at the Data Motion / Custom SQL and later sections - this is might the way to achieve your desired result. 您应该看一下“代码优先迁移” ,在“ 数据运动/自定义SQL”和后面的章节中会更详细地介绍-这可能是达到所需结果的方法。 Your migration class can look like this: 您的迁移类如下所示:
public partial class AddUniqueConstrains : DbMigration
{
public override void Up()
{
Sql("ALTER TABLE Account ADD CONSTRAINT UQ_Account_AccountNumber UNIQUE(AccountNumber)");
Sql("ALTER TABLE Account ADD CONSTRAINT UQ_Account_GUID UNIQUE(GUID)");
}
public override void Down()
{
Sql("ALTER TABLE Account DROP CONSTRAINT UQ_Account_AccountNumber UNIQUE");
Sql("ALTER TABLE Account DROP CONSTRAINT UQ_Account_GUID");
}
}
You can also explore other options described in answers to this question: Unique Constraint in Entity Framework Code First 您还可以探索此问题的答案中描述的其他选项: 实体框架代码优先的唯一约束
If you don't use (or cannot use) EF migrations you can use custom initializer as mentioned in this answer . 如果您不使用(或不能使用)EF迁移,则可以使用此答案中提到的自定义初始化程序。 The custom initializer will execute a Seed
method after creating the database = only once when database doesn't exist. 自定义初始化程序将在创建数据库后执行Seed
方法=仅在数据库不存在时执行一次。 If you need to incrementally develop the database initializer itself will not help you (that is what migrations are for). 如果您需要增量开发数据库初始化程序本身,将无济于事(这就是迁移的目的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.