简体   繁体   English

ForeignKey String ToLower实体框架

[英]ForeignKey String ToLower Entity Framework

I have a problem with the ForeignKey, as you will see in the database table i have the following value "LA URBINA" in the column "EQU". 我的外键有问题,正如您将在数据库表中看到的那样,我在“ EQU”列中具有以下值“ LA URBINA”。 in my model the property is: 在我的模型中,该属性是:

[ForeignKey("Equipo")]
public string EQU { get; set; }

private Equipo equipo;
public virtual Equipo Equipo
{
    get { return equipo; }
    set { equipo = value; }
}

the problem is that in the table "Equipo" the key code is "La Urbina" not "LA URBINA" and therefore does not make the relationship, any idea to make the comparison regardless if the value is lower or upper? 问题是在表“ Equipo”中,关键代码是“ La Urbina”而不是“ LA URBINA”,因此没有建立关系,不管值是低还是高,是否有进行比较的想法? please excuse my bad English, I hope you understand me and you can help me 请原谅我的英语不好,希望你能理解我,你可以帮助我

You must check what "Collation" your database is using. 您必须检查数据库正在使用什么“排序规则”。

For example, "Latin1_General_100_CS_AI" - the CS indicates that strings in your database is "case-sensitive" and string like "LA URBINA" and "La Urbina" are different. 例如,“ Latin1_General_100_CS_AI”-CS表示数据库中的字符串区分大小写,并且诸如“ LA URBINA”和“ La Urbina”之类的字符串不同。

If it is ""Latin1_General_100_CI_AI" - the CI indicates that string in your database are "case-insensitive" and there is no differences between those two strings. 如果它是““ Latin1_General_100_CI_AI”-CI表示数据库中的字符串是“不区分大小写”的,并且这两个字符串之间没有区别。

In order to change your database collation, re-create it (nasty scenario) or right click -> options -> collation. 为了更改数据库排序规则,请重新创建它(讨厌的场景)或右键单击->选项->排序规则。

just in case this can help someone my solution was 万一这可以帮助某人,我的解决方案是

in the model: 在模型中:

private string equ;
[ForeignKey("Equipo")]
public string EQU
{
    get { return equ.Capitalize(); }
    set { equ = value; }
}

private Equipo equipo;
public virtual Equipo Equipo
{
    get { return equipo; }
    set { equipo = value; }
}

public static string Capitalize(this string str)
{
    if (str != null || str == "")
    {
        str = str.ToLower();
        str = Regex.Replace(str, @"\b[a-z]", delegate(Match m)
        {
            return m.Value.ToUpper();
        });
    }
    return str;
}

in the class Equipo: 在Equipo类中:

private string codigo;
[Key]
[StringLength(20)]
public string Codigo
{
    get { return codigo.Capitalize(); }
    set { codigo = value; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM