EF5 code首先:要检测是否要创建一个数据库,这样我就可以运行ALTER语句 [英] EF5 Code First: Detect if it creates a database so I can run ALTER statements
问题描述
我是新与实体框架5.我们的团队正在使用 code首先
的工作流程。
I am new with Entity Framework 5. Our team is using Code First
workflow.
在我开始与我的主要问题,的让我先告诉你我已经尝试的(所有时间的最终意见:D
的)。
Before I'll start with my main question, let me first show you what I have tried (the ultimate comment of all time :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
是继承了 CBDContext
包含重写方法并且还继承了<我已创建的类code>的DbContext 。其中一个我遇到的问题是,实体框架不处理领域的独特性。我已阅读的文章配置/映射属性,并用流利的API 类型他们的网站上,我找不到任何配置属性设置成独一无二的。
MyDBContext
is the class I have created that inherits from CBDContext
that contains override methods and which also inherits from 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.
所以,我为了设置领域独树一帜的手动运行创建过程中的几个ALTER SQL语句一样,
So what I did in order to set the field unique is to manually run several ALTER sql statements during creation,
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 ...
}
}
我的问题:
- 我是正确的,实体框架的没有任何配置或数据注解的到的设置领域独树一帜的?
- 有没有办法在运行时发现或知道是否EF创建一个数据库或不这样我就可以移动或隐藏这一说法
如果(_context.Database.CreateIfNotExists())
某处,可重写的可利用的方法?
- Am I right that entity framework don't have any configuration or data annotations to set the field unique?
- Is there a way to detect or know during runtime if EF creates a database or not so I can move or hide this statement
if (_context.Database.CreateIfNotExists())
somewhere to an available method that can be overriden?
我真正想要的是移除如果(_context.Database.CreateIfNotExists())
从使用statemnt并把它放在别的地方或内部 MyDBContext
所以我的code看起来像这样,
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,
using (MyDBContext _context = new MyDBContext(connString))
{
Account _acc = new Account()
// ...Account properties ...
_context.Account.Add(_acc);
_context.SaveChanges();
}
感谢。
推荐答案
如果你不使用(或无法使用),EF迁移,你可以使用自定义的初始化中所提到的这个答案。自定义初始化将创建数据库之后执行种子
方法=只有一次,当数据库中不存在。如果您需要逐步开发数据库初始化本身不会帮助你(这就是迁移是)。
If you don't use (or cannot use) EF migrations you can use custom initializer as mentioned in this answer. The custom initializer will execute a Seed
method after creating the database = only once when database doesn't exist. If you need to incrementally develop the database initializer itself will not help you (that is what migrations are for).
这篇关于EF5 code首先:要检测是否要创建一个数据库,这样我就可以运行ALTER语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!