首先,设置数据库排序规则 [英] Code-first, setting database collation

查看:130
本文介绍了首先,设置数据库排序规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有办法配置在EF创建数据库时要使用的归类?但是在创建表之前?




  • 实体框架6.1.1

  • MS SQL 2012


解决方案

我通过自己创建DB来解决问题。基类然后将表创建到空DB中:

  public class MyInitializer:CreateDatabaseIfNotExists< MasterDataModel> 
{
public override void InitializeDatabase(MasterDataModel context)
{
if(!context.Database.Exists())
{
using(SqlConnection connection = new SqlConnection(YourConnectionString))
{
connection.Open();
using(SqlCommand command = new SqlCommand(string.Format(CREATE DATABASE {0} COLLATE Latin1_General_CI_AS,NameOfDatabase),connection))
{
command.ExecuteNonQuery();
}
}

SqlConnection.ClearAllPools();
}

base.InitializeDatabase(context);
}
}


Is there any way to configure what collation to use when EF creates the database?

Or is there some kind of hook to set the collation after the database is created but before the tables are created?

  • Entity Framework 6.1.1
  • MS SQL 2012

解决方案

I solved the issue by creating the DB myself. The base class is then creating the tables into the empty DB:

    public class MyInitializer : CreateDatabaseIfNotExists<MasterDataModel>
    {
        public override void InitializeDatabase(MasterDataModel context)
        {
            if(!context.Database.Exists())
            {
                using (SqlConnection connection = new SqlConnection("YourConnectionString"))
                {
                    connection.Open();
                    using(SqlCommand command = new SqlCommand(string.Format("CREATE DATABASE {0} COLLATE Latin1_General_CI_AS", "NameOfDatabase"), connection))
                    {
                        command.ExecuteNonQuery();
                    }
                }

                SqlConnection.ClearAllPools();
             }

             base.InitializeDatabase(context);
        }
    }

这篇关于首先,设置数据库排序规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆