Reputation: 11019
I am attempting to use Entity Framework with a database that does not make use of foreign keys. To make matters worse, tables that have a relationship make use of relating fields that can be null. For example the Order
table has a column for ClientID
that is nullable
. Keeping in mind I did not create this database nor can I change it I would like to still be able to use Entity Framework (v6.1) to perform CRUD operations on the database.
The problem, as you might expect, has to do with the association between entities. I can create the associations myself manually but I cannot set the referential integrity because the properties in the entities map to columns in the database that can be NULL
.
Is there a way to get around this? Perhaps a way to use the associations without having to define referential integrity? Granting that it would be up to me to make sure that any child records be deleted if a given parent is deleted..
Upvotes: 1
Views: 280
Reputation: 8171
I think this will work:
public class Order
{
//other properties
public int? ClientId {get; set;}
}
And set
modelBuilder.Entity<Order>()
.HasOptional(x=>x.Client)
.WithMany(x=>x.Orders)
.WithForeignKey(x=>x.CliendId);
Upvotes: 1