I'm having some troubles when saving in the database a model with a little complex relationship.
The UML of the classes is:
The classes definition are:
public abstract class EntityBase
{
public virtual Guid Id { get; set; }
}
public class LoanRequest : EntityBase
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public virtual Applicant Applicant1 { get; set; }
public virtual Applicant Applicant2 { get; set; }
}
public class Applicant
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public Guid LoanRequestId { get; set; }
[ForeignKey("LoanRequestId")]
public virtual LoanRequest LoanRequest { get; set; }
public virtual ICollection<MonthlyIncome> Incomes { get; set; }
}
public class MonthlyIncome
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public Guid ApplicantId { get; set; }
[ForeignKey("ApplicantId")]
public virtual Applicant Applicant { get; set; }
}
I'm able to run a migration and looking into the database the tables and columns created by the framework seems fine to me. But, when saving an exception happens. The exception is:
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values
I've been searching for a solution on the internet and I can't see where my problem is. Any suggestions? Thanks!
After several tryings I was able to find a solution. Changing the applicant definition to:
public class Applicant
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public virtual ICollection<MonthlyIncome> Incomes { get; set; }
}
was all I needed
Related
I am working on a Restaurant Application. I have a restaurant model and a table model.
namespace Restaurant.Models
{
[Table("Restaurant")]
public class RestaurantModel
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("name")]
public string Name { get; set; }
[Column("telephone_number")]
public int TelephoneNumber { get; set; }
[NotMapped]
public List<TableModel> Tables;
public RestaurantModel()
{
Tables = new List<TableModel>();
}
}
}
namespace Restaurant.Models
{
[Table("Table")]
public class TableModel
{
[Key]
[Column("id")]
public int Id { get; set; }
[ForeignKey("restaurant_id")]
[Required] [NotNull]
public int RestaurantId { get; set; }
[Column("available_seats")]
public int AvailableSeats { get; set; }
[Column("is_indoors")]
public bool IsIndoors { get; set; }
}
}
I have a dependency between Restaurant and Table:
Here are the columns and keys that Entity Framework has created for me via my context:
Lastly, here's my Context class:
namespace Restaurant.Data
{
public class RestaurantContext : DbContext
{
public RestaurantContext(DbContextOptions<RestaurantContext> options) : base(options)
{
}
public DbSet<RestaurantModel> Restaurants { get; set; }
public DbSet<TableModel> Tables { get; set; }
public DbSet<GuestModel> Guests { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RestaurantModel>().ToTable("Restaurant");
modelBuilder.Entity<TableModel>().ToTable("Table");
modelBuilder.Entity<GuestModel>().ToTable("Guest");
modelBuilder.Entity<TableModel>()
.HasOne<RestaurantModel>();
}
}
}
When I retrieve a restaurant, I want the corresponding tables to be retrieved inside of the TableModel List. Currently, when I retrieve a Restaurant, it will not retrieve any corresponding Tables. This makes sense to me, as I have not properly connected the relationship for EntityFramework to recognize it. I have tried to look online how to do it, consulting guides on setting up Foreign Key relationships and such. I cannot find the information I am looking for, due to a lack of basic knowledge. The answers I can find do not make sense to me because I do not understand what they are doing or how they are doing it.
Could anyone point me in the right direction or tell me what I am doing wrong?
add relations to your classes
[Table("Restaurant")]
public class Restaurant
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("name")]
public string Name { get; set; }
[Column("telephone_number")]
public int TelephoneNumber { get; set; }
public virtual ICollection<Table> Tables { get; set; }
}
}
[Table("Table")]
public class Table
{
[Key]
[Column("id")]
public int Id { get; set; }
public int? RestaurantId { get; set; }
public virtual Restourant Restaurant { get; set; }
[Column("available_seats")]
public int AvailableSeats { get; set; }
[Column("is_indoors")]
public bool IsIndoors { get; set; }
}
}
and since you are using Net core 5+ I don' t think that you any navigation attributes or fluent APIs
Delete old migration folde and make a clean migration to db
after this you can try this code for test
var restourant= context.Restourants.Include(r=> r.Tables).FirstOrDefault(r=>r.Id==id);
it should return a restourant with a list of tables
I have the following models in my .NET Core 2.1 application:
public class Product
{
[Key]
public Guid ProductId { get; set; }
public virtual ICollection<Favourite> Favourites { get; set; }
}
public class Retailer
{
[Key]
public Guid BusinessId {get; set;}
public virtual ICollection<Favourite> Favourites { get; set; }
}
public class Favourite
{
[Key]
public Guid FavouriteId { get; set; }
[ForeignKey("Retailer_BusinessId")]
public virtual Retailer Business { get; set; }
[ForeignKey("Product_ProductId")]
public virtual Product Product { get; set; }
}
When trying to run an EF migration, I get the following error:
The relationship from 'Favourite.Business' to 'Retailer.Favourites'
with foreign key properties {'Retailer_BusinessId' : Nullable}
cannot target the primary key {'BusinessId' : Guid} because it is not
compatible. Configure a principal key or a set of compatible foreign
key properties for this relationship.
I suspect it's because I'm using Guid's as my keys in the foreign tables. How do I tell EF that?
It looks like EF expects the ForeignKey attribute to refer to the navigation property. I was able to reproduce the error you were getting and after making the following change, it migrated successfully.
public class Favourite
{
[Key]
public Guid FavouriteId { get; set; }
[ForeignKey("Business")]
public Guid BusinessId { get; set; }
[ForeignKey("Product")]
public Guid ProductId { get; set; }
public virtual Retailer Business { get; set; }
public virtual Product Product { get; set; }
}
EDIT: The way you were doing it was close, but you need to have the property and then specify just the property's name in the ForeignKey attribute.
public class Favourite
{
[Key]
public Guid FavouriteId { get; set; }
public Guid BusinessId { get; set; }
public Guid ProductId { get; set; }
[ForeignKey("BusinessId")]
public virtual Retailer Business { get; set; }
[ForeignKey("ProductId")]
public virtual Product Product { get; set; }
}
I reproduced a simple example of a problem I have with Entity Framework.
I want to have three tables:
Users, Projects, WorkOrders
Table Users has information about users for all other tables (in example only two). WorkOrders has information about which User has to work on this work order and to which Project it belongs.
Here are the classes:
public class User
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<WorkOrder> WorkOrders { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
public class Project
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int ManagerId { get; set; }
public DateTime Start { get; set; }
public virtual User Manager { get; set; }
public virtual ICollection<WorkOrder> WorkOrders { get; set; }
}
public class WorkOrder
{
[Key]
public int Id { get; set; }
public int Number { get; set; }
public string Type { get; set; }
public int AssigneeId { get; set; }
public int ProjectId { get; set; }
public virtual Project Project { get; set; }
public virtual User Assignee { get; set; }
}
When I try to run the program, it throws an exception:
'Introducing FOREIGN KEY constraint 'FK_dbo.WorkOrders_dbo.Projects_ProjectId' on table 'WorkOrders' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Then I went another way. I tried DB-first approach with EF. I first created the tables and connections in SQL Server Management Studio:
Then the generated models by EF look almost the same as mine, with code-first approach.
public partial class User
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public User()
{
this.Project = new HashSet<Project>();
this.WorkOrder = new HashSet<WorkOrder>();
}
public int Id { get; set; }
public string Name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Project> Project { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<WorkOrder> WorkOrder { get; set; }
}
public partial class Project
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Project()
{
this.WorkOrder = new HashSet<WorkOrder>();
}
public int Id { get; set; }
public string Name { get; set; }
public int ManagerId { get; set; }
public System.DateTime Start { get; set; }
public virtual User User { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<WorkOrder> WorkOrder { get; set; }
}
public partial class WorkOrder
{
public int Id { get; set; }
public int Number { get; set; }
public string Type { get; set; }
public int AssigneeId { get; set; }
public int ProjectId { get; set; }
public virtual Project Project { get; set; }
public virtual User User { get; set; }
}
So the code is almost identical, except the SuppressMesages and constructors in WorkOrder and User classes. The second approach works.
I would like to know, what makes the difference? Also the context class is identical as my own. Where or how are defined this FK constraints or cascade delete settings?
It is unfortunate default behavior of EF code first to crate FK with on delete cascade. So while defining relations you need to simply change this setting:
//in context
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<Project>()
.HasRequired<User>(s => s.User)
.WithMany()
.WillCascadeOnDelete(false);
Also there might be a convention for it in modelBuilder.Conventions if you want to just change behavior for all FK.
Very simple question but it looks like I'm trying to implement a simple one-to-many relationship between two models.
So far, what I have is this :
A product class :
public class Products
{
public long Id { get; set; }
public long Code { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public Boolean Reviewed { get; set; }
[ForeignKey("BundleId")]
public int BundleId { get; set; }
public virtual Bundles Bundle { get; set; }
}
And the Defects class looks like this:
public class Defects
{
public long Id { get; set; }
public String Description { get; set; }
public String Picture { get; set; }
public DateTime DateCreated { get; set; }
[ForeignKey("ProductId")]
public int ProductId { get; set; }
public Products Product { get; set; }
[ForeignKey("UserId")]
public int UserId { get; set; }
public virtual Users User { get; set; }
}
I thought that I did not need to add an ICollection of Defects to the Products class because it's a "simple" one-to-many relationship and this code would be enought to be able to get the ID of a Product in the Defects class (I don't need more).
But, of course I get an exception :
The property 'ProductId' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter
How may I solve that issue ?
I might be doing someting wrong with my two foreign keys but since I declared the name of the foreign keys, I assumed it would have been enought.
Thanks for your attention.
This is what your relationship can be distilled to.
Please note that ForeignKey annotation is applied to navigation property with the name of the key property.
If you build one-to-many relationship - then ICollection is absolutely necessary. Otherwise where's the "many"
public class Products
{
public int Id { get; set; }
public virtual List<Defects> Bundle { get; set; }
}
public class Defects
{
public long Id { get; set; }
public int ProductId { get; set; }
[ForeignKey("ProductId")]
public Products Product { get; set; }
}
FK can also be applied to the key property. But in that case you have to put the name of the instance of related class there
public class Defects
{
public long Id { get; set; }
[ForeignKey("Product")]
public int ProductId { get; set; }
public Products Product { get; set; }
}
I have a business case that requires as much as possible to implement a database schema for the application which prevents an incorrect state for an entity called Trailers to be in more than one location entity, called Bays or Lots.
The code snippets for relevant parts of the entity models are below.
Essentially, my problem is I cannot figure out how to create a schema that will prevent a trailer from being in a lot, and in a Bay at the same time, while also keeping the 1 to many relationship from lots to trailers and 1 to 1 for bays trailers.
I can obviously do things to mitigate this problem in the application, but having a schema that prevents this state is better.
Anyone have any thoughts on how this might be best accomplished?
public class Trailer
{
public int TrailerId { get; set; }
public MaintStatuses MaintStatus { get; set; }
....
}
public class Location
{
[Key]
public string LocationName { get; set; }
//Navigation
public virtual ICollection<Bay> LocationBays { get; set; }
public virtual Lot ParkingLot { get; set; }
...
}
public class Bay
{
[Key]
public int Id { get; set; }
public string BayName { get; set; }
//Navigation
public virtual Trailer Trailer { get; set; }
public virtual Location Location { get; set; }
...
}
public class Lot
{
[Key]
public string LotName { get; set; }
[ForeignKey("Location")]
public string LocationName { get; set; }
public virtual Location Location { get; set; }
public virtual ICollection<Trailer> ParkedTrailers{get; set;}
...
}