What are the errors in this code using EF6?

1

What are the errors or improper practices in this code?

static void Main(string[] args)
{
    using (var db = new AccountingSystemContainer())
    {
        var invHeader = db.InvoiceHeaderSet.Create();
        var invDetail = db.InvoiceDetailSet.Create();

        invHeader.Total = 150m;

        invDetail.ItemDescription = "Algum Item";
        invDetail.Price = 75m;
        invDetail.Quantity = 2;

        invHeader.InvoiceDetail.Add(invDetail);

        db.InvoiceHeaderSet.Add(invHeader);
        db.SaveChanges();
    }
}

public partial class InvoiceDetail
{
    public int InvoiceDetail_Id { get; set; }
    public string ItemDescription { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
    public int InvoiceHeaderInvoiceHeader_Id { get; set; }

    public virtual InvoiceHeader InvoiceHeader { get; set; }
}

public partial class InvoiceHeader
{
    public InvoiceHeader()
    {
        this.InvoiceDetail = new HashSet<InvoiceDetail>();
    }

    public int InvoiceHeader_Id { get; set; }
    public decimal Total { get; set; }

    public virtual ICollection<InvoiceDetail> InvoiceDetail { get; set; }
}

public partial class AccountingSystemContainer : DbContext
{
    public AccountingSystemContainer()
        : base("name=AccountingSystemContainer")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<InvoiceHeader> InvoiceHeaderSet { get; set; }
    public virtual DbSet<InvoiceDetail> InvoiceDetailSet { get; set; }
}
    
asked by anonymous 10.09.2015 / 01:16

1 answer

2

1. Key Nomenclature

The names used for primary keys are outside the standard Entity Framework Ids search convention . The correct would be:

public partial class InvoiceDetail
{
    public int InvoiceDetailId { get; set; }
    ...
}

public partial class InvoiceHeader
{
    public int InvoiceHeaderId { get; set; }
    ...
}

Or else:

public partial class InvoiceDetail
{
    public int Id { get; set; }
    ...
}

public partial class InvoiceHeader
{
    public int Id { get; set; }
    ...
}

Or, if the bank already exists and you want to use names that are outside the convention, you can use this:

[Table("INVOICE_DETAIL")]
public partial class InvoiceDetail
{
    [Key]
    [Column("InvoiceDetail_Id")]
    public int InvoiceDetail_Id { get; set; }
    ...
}

[Table("INVOICE_HEADER")]
public partial class InvoiceHeader
{
    [Key]
    [Column("InvoiceHeader_Id")]
    public int InvoiceHeader_Id { get; set; }
    ...
}

2. Initializing Navigation Properties in the Constructor

The Entity Framework initializes navigation properties by itself. You do not have to initialize anything, even because this effort is useless. The Entity Framework will reassign the navigation properties when it deems it necessary:

public partial class InvoiceHeader
{
    // Retire
    /* public InvoiceHeader()
    {
        this.InvoiceDetail = new HashSet<InvoiceDetail>();
    } */
    ...
}

3. UnintentionalCodeFirstException in < a href="https://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext.onmodelcreating(v=vs.113).aspx"> OnModelCreating

Here it is not well error: it is a remark. The code below prevents you from making any structural changes to the database:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    throw new UnintentionalCodeFirstException();
}

As you are possibly using Model / Database First to map a database that already exists, it is pertinent to keep the annotation just to ensure that the Entity Framework will not make any changes to the database.

However, it is important to note that if the database is not identical to the Models mapped in your application, the chance of error is quite large.

4. Other Details

Here are not errors. These are observations that may be pertinent to their development.

It is not clear to me why naming a data context as AccountingSystemContainer . It does not make it clear to the programmer that it is a DbContext .

DbSet s does not have Set at the end. If they are properties that belong to the context, they are most likely to be DbSet s (with some exceptions, such as Database ").

    
10.09.2015 / 01:54