0

我是 Entity Framework 5 的新手。我们的团队正在使用Code First工作流。

在我开始我的主要问题之前,让我首先向您展示我尝试过的东西 有史以来的终极评论: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包含重写方法并且也继承自DBContext. 我遇到的问题之一是实体框架不处理字段唯一性。我已经在他们的网站上阅读了关于使用 Fluent API 配置/映射属性和类型的文章,但我找不到任何配置来将属性设置为唯一。

所以我为了设置字段唯一所做的就是在创建过程中手动运行几个 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 ...
    }
}

我的问题:

  1. 我对实体框架没有任何配置或数据注释设置字段唯一性是否正确?
  2. 有没有办法在运行时检测或知道 EF 是否创建了数据库,以便我可以将此语句移动或隐藏if (_context.Database.CreateIfNotExists())到可以覆盖的可用方法中?

我真正想要的是if (_context.Database.CreateIfNotExists())从 using statemnt 中删除并将其放在其他地方或内部MyDBContext,这样我的代码将如下所示,

using (MyDBContext _context = new MyDBContext(connString))
{
    Account _acc = new Account()
    // ...Account properties ...

    _context.Account.Add(_acc);
    _context.SaveChanges();
}

谢谢。

4

2 回答 2

4

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. 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

于 2013-04-18T14:04:03.467 回答
1

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).

于 2013-04-18T15:09:07.020 回答