实体框架代码第一和Firebird - 外键名称问题 [英] Entity Framework Code First and Firebird - Foreign Key name issue

查看:227
本文介绍了实体框架代码第一和Firebird - 外键名称问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用EF代码优先创建具有两个表(区域数据库)的新数据库和这个简单的代码:

I'm trying to create new database with 2 tables (Districts and Databases) using EF code-first and this simple code:

using (var db = new FirebirdDBContext(_connectionString))
{
    db.Database.CreateIfNotExists();
}

我的课程:

public class District
{
    [Key]
    public int District_id { get; set; }

    [Column(TypeName = "VARCHAR")]
    [StringLength(50)]
    public string District_name { get; set; }

    [ForeignKey("DataBase")]
    public int DataBase_id { get; set; }

    public DataBase DataBase { get; set; }
}

public class DataBase
{
    [Key]
    public int DataBase_id { get; set; }

    [Column(TypeName = "VARCHAR")]
    [StringLength(50)]
    public string DataBase_name { get; set; }

    public ICollection<District> District { get; set; }

    public DataBase()
    {
        District = new List<District>();
    }
}    

但不幸的是,它会抛出一个错误:

But unfortunately it throws an error:


指定的参数超出有效值的范围。

参数名称:名称FK_Districts_DataBases_DataBase_id长于Firebird的31个字符限制对象名称。

Specified argument was out of the range of valid values.
Parameter name: The name 'FK_Districts_DataBases_DataBase_id' is longer than Firebird's 31 characters limit for object names.

我知道Firebird的31个字符限制,但如果我使用Entity Framework代码, ?

I know about Firebird's 31 characters limit but how I can solve this problem if I use Entity Framework code-first?

推荐答案

增加元数据字段名称的Firebird 31个字符限制是一个常量的功能请求,没有真正的方法来增加长度限制。

Increasing Firebird's 31 character limit for metadata field names has been a constant feature request, and there is no real way to increase the length limit.

我们可以控制外键约束名称的长度,EF试图生成..这将涉及重命名你的类属性到更短的名字(但仍然将它们映射到它们的真正的列名)

Having said that, we might be able to control the length of the foreign-key constraint name, that EF is trying to generate.. this will involve renaming your class properties to shorter names (but still map them to their true column names)

希望EF生成外键约束名称关闭类的属性名称,而不是

Hopefully EF generates the foreign-key constraint names off the class' property names, and not the actual columns.

FK_Districts_DataBases_DataBase_id为23 + 11 = 34个字符。
我们需要得到32个字符以下。

FK_Districts_DataBases_DataBase_id is 23 + 11 = 34 characters. we need to get it under 32 characters..

我认为这个前缀可能是不可避免的。

i think this prefix maybe unavoidable. FK_Districts_DataBases_ so we have leeway for an 7-8 character suffix.

请尝试以下操作:

public class District
{
    [Key]
    public int District_id { get; set; }

    [Column(TypeName = "VARCHAR")]
    [StringLength(50)]
    public string District_name { get; set; }

    [ForeignKey("DataBase")]
    [Column("DataBase_id")]
    public int DbId { get; set; } // reduce the column name

    public DataBase DataBase { get; set; }
}

public class DataBase
{
    [Key]
    [Column("DataBase_id")]
    public int DbId { get; set; } // reduce the column name

    [Column(TypeName = "VARCHAR")]
    [StringLength(50)]
    public string DataBase_name { get; set; }    

    public ICollection<District> District { get; set; }

    public DataBase()
    {
        District = new List<District>();
    }
} 

希望EF使约束名称为'FK_Districts_DataBases_DbId' 27个字符)

hopefully EF makes the constraint name as 'FK_Districts_DataBases_DbId' (27 characters)

这篇关于实体框架代码第一和Firebird - 外键名称问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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