vendredi 8 juillet 2016

Entity Framework Set Navigation Property using Reflection

I built an Excel importer that is dynamic. Using reflection, its assigning the values to a new instance of an EF object and adding the EF object to the DbSet.

public class Lease
{
    public int Id { get; set; }
    public int StatusId { get; set; }

    public LeaseStatus { get; set; }
}

public class LeaseStatus
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Lease> Leases { get; set; }
}

PROBLEM:

When setting Lease.LeaseStatus property via reflection, I get a System.Data.Entity.Infrastructure.DbUpdateExceptionback when saving the DbSet.

INNER EXCEPTION:

System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Leases_LeaseStatuses". The conflict occurred in database "MyDatabaseName", table "dbo.LeaseStatus", column 'Id'. The statement has been terminated.

QUESTION:

How can I set a navigation property via reflection? If I'm doing it correctly, what may be causing the Exception?





Aucun commentaire:

Enregistrer un commentaire