Mapping Table in DB first without foreign key - c#

I have a program written with a database-first approach; I have a table ServicePlan and another ServicePlanDetails. They are not mapped to each other, but they have a common column PlanId; a servicePlan can contain multiple ServicePlanDetails like a list of it.
I don't want to make any change to the database, but I want to map them as well. How can I do this? Does doing this within the method of on model creating will do the work for me and will not change anything in the database? I have tried this but could get the result.
For simplicity, I have just added few columns and their mapping and not all of them:
public partial class ServicePlan
{
public ServicePlan()
{
ServicePlanDetails = new HashSet<ServicePlanDetail>();
}
public long PlanId { get; set; }
public decimal PhoneId { get; set; }
public byte? NLines { get; set; }
public DateTime? DateStart { get; set; }
public DateTime? DateEnd { get; set; }
public virtual ICollection<ServicePlanDetail> ServicePlanDetails { get; set; }
}
public partial class ServicePlanDetail
{
public long PlanId { get; set; }
public string? ServCode { get; set; }
public string? CountryCode { get; set; }
public bool? IsPlan { get; set; }
public decimal? Cost { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ServicePlan>(entity =>
{
entity.HasKey(e => e.PlanId).HasName("PK_UsersPlan");
entity.ToTable("ServicePlan");
entity.HasIndex(e => e.VideoTronId, "IDX_VTID").HasFillFactor(80);
entity.HasIndex(e => new { e.PhoneId, e.IsApproved }, "Ix_SrvcPlan").HasFillFactor(80);
entity.Property(e => e.Zone).HasMaxLength(50);
entity.HasMany(p => p.ServicePlanDetails)
.WithOne()
.HasPrincipalKey(p => p.PlanId)
.HasForeignKey(p => p.PlanId);
});
}
The error I get is :
Unable to determine the relationship represented by navigation 'ServicePlan.ServicePlanDetails' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'
I want to get the serviceplandetails with the same planid as the serviceplan into a list in the serviceplan.

PlanId cannot be both foreign and principal key for one to many.
public partial class ServicePlanDetail
{
public long Id { get; set; }
public long PlanId { get; set; }
public string? ServCode { get; set; }
public string? CountryCode { get; set; }
public bool? IsPlan { get; set; }
public decimal? Cost { get; set; }
}
Configuration
entity.HasMany(p => p.ServicePlanDetails)
.WithOne()
.HasPrincipalKey(p => p.PlanId)
.HasForeignKey(p => p.PlanId);

If in the database a Plan can have many ServicePlanDetails, and you link them by Plan ID, how do you differentiate one ServicePlanDetail against that Plan from another? What makes two ServicePlanDetail records unique? That is the crux of your problem. Your FK mapping is correct, but it won't work if PlanId is the PK on ServicePlanDetail. PKs must uniquely identify a single record. For instance if your plan is associated to service plan details applying to various users where multiple users reference the same plan and there is a UserID on ServicePlanId, the PK should be a composite of PlanId + UserId.
As a DB-First approach the database should already have the PKs and constraints set up. You just set up EF keys and relationship types to match that.
Now if the ServicePlanDetail's PK is declared as just PlanId, then the answer is that the relationship between Plan and ServicePlanDetail is 1-to-1, not 1-to-many. This becomes a .HasOne(p => p.ServicePlanDetail).WithOne(sp => sp.Plan) and there's really nothing you can do about that without altering the data relationships. You cannot magically change the relationship that EF will use if the underlying database schema cannot support that relationship.

Related

How to have foreign key and related navigation property with non-standard name

In Entity Framework Core (v5.0.6) I'm trying to create a relationship where the object has both a foreign key id field AND a navigation property both of which have non-standard names but when I try to build a migration, I'm getting an error saying Unable to determine the relationship represented by navigation 'Org.IntegrationAdmin' of type 'OrgUser'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'
My code is:
public class OrgUser
{
public Guid Id { get; set; }
public string Name { get; set; }
public Guid OrgId { get; set; }
public virtual Org? Org { get; set; }
}
public class Org
{
[ForeignKey(nameof(OrgUser))]
public Guid IntegrationAdminId { get; set; }
[ForeignKey(nameof(IntegrationAdminId))]
public virtual OrgUser? IntegrationAdmin { get; set; }
}
The reason for the non-standard names is that the Org class will actually have several foreign key / navigation properties in it so using standard naming is not an option.
I would rather do this with attributes rather than fluent syntax.
I want to have both the id and the navigation property as sometimes will want to include the navigation property but others, just want the id, which will be useful for comparing records without the overhead of doing a db join.
Update:
Sorry for wasting peoples time but I made the classic mistake of inadvertently editing out the important part of my code 🤦‍♂️🤦‍♂️🤦‍♂️
I've added the reference to Org into OrgUser that was confusing EF!
Try this
public class OrgUser
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
[InverseProperty(nameof(Org.IntegrationAdmin))]
public virtual ICollection<Org> Orgs { get; set; }
}
public class Org
{
[Key]
public Guid Id { get; set; }
public Guid? IntegrationAdminId { get; set; }
[ForeignKey(nameof(IntegrationAdminId))]
[InverseProperty("Orgs")]
public virtual OrgUser IntegrationAdmin { get; set; }
}
or you can try dbcontext
modelBuilder.Entity<Org>(entity =>
{
entity.HasOne(d => d.IntegrationAdmin)
.WithMany(p => p.Orgs)
.HasForeignKey(d => d.IntegrationAdminId )
.OnDelete(DeleteBehavior.ClientSetNull);
});
I've found the problem, it was that in addition to the OrgUser ref in Org, there was also an Org ref in OrgUser that I'd not noticed.
This confused EF and resulted in an error message that pointed at the wrong thing and confused the hell out of me.
I removed all ForeignKey attributes and then added the following to my DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Org>(entity =>
{
entity.HasOne(o => o.IntegrationAdmin)
.WithMany();
});
modelBuilder.Entity<OrgUser>(entity =>
{
entity.HasOne(d => d.Org)
.WithMany();
});
}

Composite Shared Keys and Entity

I'm working on a database where tables have composite keys and part of that key is shared between tables. I don't know how to set the relationship properly in entity.
Imagine the following:
public class Sale
{
public long ID { get; set; } //Key
public long RetailerID { get; set; } //Key
public virtual Location Location { get; set; } //Foreign, Many-to-One
}
public class Location
{
public long ID { get; set; } //Key
public long RetailerID { get; set; } //Key
public virtual IEnumerable<Sale> Sales { get; set; } //Relationship, One-to-Many
}
Both are using the fluent API to define the composite keys OnModelCreating.
modelBuilder.Entity<Sale>().HasKey(x => new { x.RetailerID, x.ID });
modelBuilder.Entity<Location>().HasKey(x => new { x.RetailerID, x.ID });
However I am unsure how to finish this to set up the proper relationship as it sets itself up as having duplicate columns for RetailerID which is unnecessary. How is this supposed to be done properly (if at all?)
It's possible in several ways, all including additional LocationID FK property (either explicit or shadow).
With shadow FK property (without modifying the entity model):
Data Annotations:
[Required]
[ForeignKey("RetailerID, LocationID")]
public virtual Location Location { get; set; } //Foreign, Many-to-One
Fluent API:
modelBuilder.Entity<Sale>()
.HasOne(e => e.Location)
.WithMany(e => e.Sales)
.HasForeignKey("RetailerID", "LocationID")
.IsRequired();
with explicit FK property
Model:
public long LocationID { get; set; } // added
public virtual Location Location { get; set; } //Foreign, Many-to-One
Data annotations:
[ForeignKey("RetailerID, LocationID")]
public virtual Location Location { get; set; } //Foreign, Many-to-One
Fluent API:
modelBuilder.Entity<Sale>()
.HasOne(e => e.Location)
.WithMany(e => e.Sales)
.HasForeignKey(e => new { e.RetailerID, e.LocationID });
(note: use either data annotations or fluent API - no need for both)

Entity Framework Core One To Many with No Foreign Key on Master Table

We've decided to consolidate all enums into a RefValue table which I at least have no problem conceiving of, but I can't seem to get Entity to play along.
I think it should make sense once you look at the schema, but the gist of it is that a Person object would have 2 fields when it came to enums (eg gender, relationshipStatus, nameSuffix, etc). One field would store the value (eg "female") and the other would store the id of that RefValue (eg some guid which would be a foreign key to the RefValue table where the value was stored/defined). That way we wouldn't have to do multiple joins on the RefValue table to find those properties values.
Here's the schema:
[Table("Person")]
public class Person : Base
{
public Guid Id {get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public RefValue GenderRef { get; set; }
public string RelationshipStatus { get; set; }
public RefValue RelationshipStatusRef { get; set; }
public string NameSuffix { get; set; }
public RefValue NameSuffixRef { get; set; }
}
[Table("RefValue")]
public class RefValue : Base
{
public Guid Id {get; set; }
public string Type { get; set; }
public string Value { get; set; }
}
In the Person object, I'd really just like the RefValue properties to just be Guids as foreign keys to the RefValue table. I started steering towards just making them RefValue properties because it seemed like EFC would make things easier if I did it their way with the concept of navigation properties. The problem though is that it's pretty insistent on having a column on the RefValue table to be the other side of those one-to-many relationships.
I'm fine with adding extra columns on the RefValue table so that is looks more like this:
[Table("RefValue")]
public class RefValue : Base
{
public string Type { get; set; }
public string Value { get; set; }
public ICollection<Person> Genders { get; set; }
public ICollection<Person> RelationshipStatus { get; set; }
public ICollection<Person> NameSuffix { get; set; }
}
But no matter how I spin my context class's OnModelCreating() method, I can't seem to get them to play together.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RefValue>()
.HasMany(p => p.Genders)
.WithOne(p => p.GenderRef)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<RefValue>()
.HasMany(p => p.RelationshipStatus)
.WithOne(p => p.RelationshipStatusRef)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<RefValue>()
.HasMany(p => p.NameSuffix)
.WithOne(p => p.NameSuffixRef)
.OnDelete(DeleteBehavior.SetNull);
}
I've tried going the inverse way as well, but I usually get the Introducing FOREIGN KEY constraint ... on table ... may cause cycles or multiple cascade paths
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>()
.HasOne(p => p.PersonTypeRef)
.WithMany()
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<Person>()
.HasOne(p => p.RelationshipStatusRef)
.WithMany()
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<Person>()
.HasOne(p => p.NameSuffixRef)
.WithMany()
.OnDelete(DeleteBehavior.SetNull);
}
Is there any way to create this kind of one-to-many (or really many-to-one) kind of relationship without the cascading problem or (and especially) entity creating shadow properties on the non-dependent table?
Let's take your original entity model:
[Table("Person")]
public class Person : Base
{
public Guid Id {get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public RefValue GenderRef { get; set; }
public string RelationshipStatus { get; set; }
public RefValue RelationshipStatusRef { get; set; }
public string NameSuffix { get; set; }
public RefValue NameSuffixRef { get; set; }
}
[Table("RefValue")]
public class RefValue : Base
{
public Guid Id {get; set; }
public string Type { get; set; }
public string Value { get; set; }
}
There is absolutely no problem to configure unidirectional (i.e. with navigation property only at one of the sides) FK relationships using the fluent API. The only thing you need to consider is that because of the multiple cascade paths introduced by such model, you can't use delete cascade options, i.e. you should specify DeleteBehavior.Restrict.
With that being said, here is the fluent configuration for the above model:
modelBuilder.Entity<Person>()
.HasOne(p => p.GenderRef)
.WithMany()
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Person>()
.HasOne(p => p.RelationshipStatusRef)
.WithMany()
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Person>()
.HasOne(p => p.NameSuffixRef)
.WithMany()
.OnDelete(DeleteBehavior.Restrict);
And in case you add collection navigation properties, don't forget to specify them in the corresponding WithMany calls, otherwise EF Core will create additional relationships.
We've decided to consolidate all enums into a RefValue table
That makes me sad. How are you going to keep people from having a gender of Single, or a relationship status of Female? How are you going to populate a drop-down with all the valid values for a particular foreign key? How are you going to help report writers from joining the tables correctly?
I can't seem to get them to play together.
That's because what you are attempting is a fundamental violation of relational design. And this is just the first of many difficulties that should make you rethink this design.

Many to one migrations fails on foreign key long

I have 2 models:
public class Text
{
public long Id { get; set; }
public string Text { get; set; }
}
public class User
{
public int Id { get; set; }
public ICollection<Text> Texts { get; set; }
}
My model build on user is that
e.HasMany(o => o.Texts).WithOne().HasForeignKey(d => d.Id).IsRequired();
When I try to run:
dotnet ef migrations add
I get this error:
with foreign key properties {'Id' : long} cannot target the primary
key {'Id' : int} because it is not compatible. Configure a principal
key or a set of compatible foreign key properties for this
relationship.
UPDATE:
It should be able for new models to have a collection of the table Texts like:
public class Customer
{
public int Id { get; set; }
public ICollection<Text> Texts { get; set; }
}
....
e.HasMany(o => o.Texts).WithOne().HasForeignKey(d => d.Id).IsRequired();
Had similar problem using EF Core but didn't want to include the (equivalent in my class) UserId on the dependent entity Text, just to make happy EF. Finally found that you can replace the primary key used in the relationship (UserId)
using HasPrincipalKey()
modelBuilder.Entity<User>()
.HasMany(t => t.Texts)
.WithOne()
.HasPrincipalKey(u => u.Text);
Firstly, change your Model naming please,
public class Text
{
public long Id { get; set; }
public int UserId { get; set; }// add a foreign key that could point to User.Id
public string Body { get; set; }//you cannot have a string property called "Text".
public virtual User Owner { get; set; }
}
public class User
{
public int Id { get; set; }
public virtual ICollection<Text> Texts { get; set; } = new HashSet<Text>();
}
builder.Entity<Text>(table =>
{
table.HasKey(x => x.Id);
table.HasOne(x => x.User)
.WithMany(x => x.Texts)
.HasForeignKey(x => x.UserId)
.HasPrincipalKey(x => x.Id)//<<== here is core code to let foreign key userId point to User.Id.
.OnDelete(DeleteBehavior.Cascade);
});
the reason we have to figure out which key is referred is because of multiple primary keys. I saw it once in MSDN, but cannot find it back.
You can use shadow properties for foreign keys, it looks popular now.
public class Text
{
public long Id { get; set; }
public string Body { get; set; }
public virtual User Owner { get; set; }
}
public class User
{
public int Id { get; set; }
public virtual ICollection<Text> Texts { get; set; } = new HashSet<Text>();
}
builder.Entity<Text>(table =>
{
table.HasKey(x => x.Id);
// Add the shadow property to the model
table.Property<int>("UserId");
table.HasOne(x => x.User)
.WithMany(x => x.Texts)
.HasForeignKey("UserId")//<<== Use shadow property
.HasPrincipalKey(x => x.Id)//<<==point to User.Id.
.OnDelete(DeleteBehavior.Cascade);
});
In the EF context configuration, specifically in the HasForeignKey() you are supposed to specify Which property on the Text model should be the foreign key that points to the User model?
Since User model's primary key is an int, the foreign key pointing from Text to User should naturally also be an int.
I think the mistake you've made is that you are configuring the PK of Textto also be the FK for the relationship Text -> User. Try to change your Text model to :
public class Text
{
public long Id { get; set; }
public string Text{ get; set; }
public int UserId { get; set; }
}
And your configuration to:
e.HasMany(o => o.Texts).WithOne().HasForeignKey(d => d.UserId).IsRequired();
You can simply drop all the migrations or the migration that made that Id, drop the database (if it is small or has no data) and add a clean migration
I was facing the same issue in one-to-one relationship. If you are facing the issue in one-one relationship. Then try this:
public partial class document
{
public document()
{
groups = new group();
}
public int? group_id { get; set; }
public virtual group groups { get; set; }
}
[Table("group")]
public class group
{
[Key]
[Column("group_id")]
public int group_id { get; set; }
[ForeignKey(nameof(group_id))]
public virtual document document { get; set; }
}
Each document has single group. So, we can consider these settings.
modelBuilder.Entity<group>().HasOne(a => a.document)
.WithOne(y => y.groups).HasForeignKey<document>(b => b.group_id);

EF5 Code first - Many to Many with extra fields

I'm trying to use the awsome EF5 with code first - where I need to make a many-to-many table with extra fields.
I've got a products table, orders table and need a table of products that are in orders with a "size" field.
What I've done is created a new class of "ProductOrder" that is the connection table between them, and made a reference.
It WORKS when creating a new order, but is not working when fetching an order - it doesn't get the connected orders (that are present in the DB after the insertion).
Ideas why? :)
My Classes are:
public class Order
{
public int ID { get; set; }
...
public ICollection<ProductOrder> Products { get; set; }
public Order()
{
Products = new HashSet<ProductOrder>();
}
}
public class Product
{
public int ID { get; set; }
public ICollection<ProductOrder> Orders { get; set; }
}
public class ProductOrder
{
public int ID { get; set; }
public int ProductID { get; set; }
public int OrderID { get; set; }
public int Size { get; set; }
[ForeignKey("OrderID")]
public Order order { get; set; }
[ForeignKey("ProductID")]
public Product product { get; set; }
}
and in onModelCreating
modelBuilder.Entity<Order>()
.HasMany(p => p.Products)
.WithRequired(o => o.order)
.HasForeignKey(o => o.OrderID);
modelBuilder.Entity<Product>()
.HasMany(o => o.Orders)
.WithRequired(p => p.product)
.HasForeignKey(p => p.ProductID);
Your navigational properties need to be virtual
From what I've seen, marking the navigational properties as virtual turns lazy loading on for those properties.
The other way to get this to work without lazy loading is to add .include() statements to your select. This tells EF to pull back the extra data that you want.

Categories

Resources