【问题标题】:EF CodeFirst - One-to-One relationship with non nullable columns - For ExpertsEF 代码优先 - 与不可为空的列的一对一关系 - 专家
【发布时间】:2012-06-19 18:33:27
【问题描述】:

我的数据库有一个来自第三方公司的约定,即“数据库中的所有列都必须是 'not null'”。

现在我正在使用 EFCodefirst 映射所有表,但遇到了问题。

例如,我有一个实体 SA1SA3 实体具有一对一关系,我想添加一个新的 SA1a1_vend 属性具有一个空字符串.

我为解决这个问题所做的就是在 PK 中添加一个带有空字符串的 SA3 实体,但我不喜欢这种方法。我想要一个更好的解决方案来解决我的问题。

我的 EFCodefirst 课程:

[ComplexType]
public class Endereco
{
    public string Logradouro { get; set; }
    public string Numero { get; set; }
    public string CEP { get; set; }
}

public class SA3
{
    public string Codigo { get; set; }
    public string Nome { get; set; }
}


public class SA1
{
    public string Codigo { get; set; }
    public string Nome { get; set; }
    public Endereco Endereco { get; set; }
    public Endereco EnderecoCobranca { get; set; }
    public bool IsDeleted { get { return false; } }

    public string a1_vend { get; set; }
    public SA3 Vendedor { get; set; }

    public SA1()
    {
        Endereco = new Endereco();
        EnderecoCobranca = new Endereco();
    }
}


public class SA3Map : EntityTypeConfiguration<SA3>
{
    public SA3Map()
    {


        ToTable("sa3010");

        HasKey(x => x.Codigo);

        Property(x => x.Codigo)
            .HasColumnName("a3_cod");

        Property(x => x.Nome)
            .HasColumnName("a3_nome");
    }
}

public class SA1Map : EntityTypeConfiguration<SA1>
{
    public SA1Map()
    {
        ToTable("sa1010");

        HasKey(x => x.Codigo);

        Property(x => x.Codigo)
            .HasColumnName("a1_cod")
            .IsRequired();
        Property(x => x.Nome)
            .HasColumnName("a1_nome")
            .IsRequired();
        Property(x => x.Endereco.Logradouro)
            .HasColumnName("a1_end")
            .IsRequired();
        Property(x => x.Endereco.Numero)
            .HasColumnName("a1_num")
            .IsRequired();
        Property(x => x.Endereco.CEP)
            .HasColumnName("a1_cep")
            .IsRequired();
        Property(x => x.EnderecoCobranca.Logradouro)
            .HasColumnName("a1_endcob")
            .IsRequired();
        Property(x => x.EnderecoCobranca.CEP)
            .HasColumnName("a1_cepcob")
            .IsRequired();
        Property(x => x.EnderecoCobranca.Numero)
            .HasColumnName("a1_numcob")
            .IsRequired();
        Property(x => x.a1_vend)
            .IsRequired();

        HasRequired(x => x.Vendedor)
            .WithMany()
            .HasForeignKey(x => new { x.a1_vend })
            .WillCascadeOnDelete(false);
    }
}

我的示例程序:

class Program
{
   static void Main(string[] args)
    {
        MyContext ctx = new MyContext();
        var novoVendedor = new SA3()
        {
            Codigo = "",
            Nome = "Empty, don´t remove this row"
        };
        ctx.Vendedores.Add(novoVendedor);

        var novoCliente = new SA1()
        {
            Codigo = "000001",
            a1_vend = "", //I can´t use null here because my database convention
            Endereco = new Endereco() { Numero = "99", CEP = "13280000", Logradouro = "Rua Teste" },
            Nome = "Cliente S/A",
            EnderecoCobranca = new Endereco { CEP = "13444999", Numero = "S/N", Logradouro = "Rua Cobranca" }
        };
        ctx.Clientes.Add(novoCliente);
        ctx.SaveChanges();



    }
}

【问题讨论】:

    标签: entity-framework entity-framework-4 ef-code-first


    【解决方案1】:

    如果以下情况属实:

    1. SA1 -> SA3 是一对一的关系。
    2. a1_vend 列在您的 sa1010 表中。
    3. 您不能将a1_vend 列设为可为空

    如果没有要首先引用的 SA3 对象,您将无法创建 SA1 对象。

    如果您不能使 a1_vend 列可以为空,您的另一个选择是从 sa1010 表中删除 a1_vend 列并创建一个映射表,将 SA1 对象映射到 SA3 对象(只需需要两列:SA1.Codigoa1_vend 我猜这与SA3.Codigo 相同)

    然后您将您的SA1Map 更改如下:

            ...
            Property(x => x.EnderecoCobranca.Numero)
                .HasColumnName("a1_numcob")
                .IsRequired();
            //Property(x => x.a1_vend) // removing this
                //.IsRequired();
    
            HasRequired(x => x.Vendedor)
                .WithMany()
                .Map(m =>
                        {
                            m.ToTable("sa1010sa3010Map");
                            m.MapLeftKey("sa1_Codigo");
                            m.MapRightKey("sa3_Codigo");
                        });
        }
    

    【讨论】:

    • 我很想按你说的做,但它没有显示 MapLeftKey 和 MapRightKey 选项,它只显示 MapKey 和 ToTable。 HasRequired(x => x.Vendor) .WithMany() .Map(m => { m.ToTable("sa1_sa3_map"); //这里不显示 } );
    猜你喜欢
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多