Entity Framework Code First Relationship Issue - entity-framework-4.1

I'm struggling to get a specific code-first relationship working correctly. Basically I have the following classes:
public class Service
{
public Guid ServiceId { get; set; }
public virtual List<ServicePackageService> ServicePackageServices { get; set; }
}
public class ServicePackageService
{
public Guid ServicePackageServiceId { get; set; }
public Guid ServiceId { get; set; }
public virtual Service Service { get; set; }
public Guid PackageServiceId { get; set; }
public virtual Service PackageService { get; set; }
}
The idea here is that a Service object can have multiple ServicePackageService objects. These PackageServiceObjects are related to a single Service, but then they have a PackageService property which is another relationship to the Service table.
In my configuration I'm using the following:
public ServicePackageServiceConfiguration()
{
HasRequired(x => x.Service);
HasRequired(x => x.PackageService).WithMany().HasForeignKey(a => a.PackageServiceId).WillCascadeOnDelete(false);
}
But the datatable that results is as follows:
ServicePackageServiceId (PK)
ServiceId (No foreign key appears on this column)
PackageServiceId (FK)
Service_ServiceId (FK)
Can anyone point me in the right direction as to how to configure this relationship.

This was all down to an error in the configuration.
The correct way to handle this relationship is to specify the following Service configuration:
HasMany(x => x.ServicePackageServices)
.WithRequired(b => b.Service)
.HasForeignKey(m => m.ServiceId);
Along with a ServicePackageService configuration for the reverse relationship:
HasRequired(x => x.PackageService)
.WithMany()
.HasForeignKey(a => a.PackageServiceId).WillCascadeOnDelete(false);

Related

MySQL / EF 6.0 Core One-to-One relationship returns null

I am using EF Core 6.0 with MySQL (Pomelo.EntityFrameworkCore.MySql v6.0)
I am setting up my database via code first. Here are my 2 models (simplified):
public class Store : BaseEntity
{
public string Name { get; set; }
public virtual User? Owner { get; set; }
public int? OwnerId { get; set; }
}
public class User : BaseEntity
{
public string? Name { get; set; }
public virtual Store? Store { get; set; }
}
For both, I have
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
as the primary key (from BaseEntity).
I also have Lazy Loading enabled here:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLazyLoadingProxies();
}
and in the Program.cs:
builder.Services.AddDbContext<AppDbContext>(opt => opt
.UseLazyLoadingProxies()
.ConfigureWarnings(warning => warning.Ignore(CoreEventId.DetachedLazyLoadingWarning))
.EnableSensitiveDataLogging()
.UseMySql(
Globals.DB_CONNECTION_STRING, new MySqlServerVersion(Globals.MYSQL_SERVER_VERSION),
o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)
));
Now, to the problem - I am trying to get a store from the database, using the following code:
Store? store = await dbContext.Stores
.Include(x => x.Owner)
.FirstOrDefaultAsync(x => x.Owner.Id == ownerId && x.Id == storeId);
I am getting the store details, but the Owner object and OwnerId is null. I can see the data in the database (e.g. I see OwnerId is set up for this specific store), but in the code, it is null.
I read on SO that Pomelo has some issues with setting up navigation properties, so I set it manually in OnModelCreating as follows:
modelBuilder.Entity<User>()
.HasOne(x => x.Store)
.WithOne(x => x.Owner)
;
But that didn't do the trick.
The same configuration works perfectly with MSSQL.
Any thoughts?
Thanks
Call HasForeignKey after WithOne and point to OwnerId.

How to create a dependency graph using composite keys in EF Core

Trying to store a composite key table which is keyed for both fields to the table it defines dependencies for.
Example case
Import files: 1..10
Dependencies 1: 2,3; 2: 4,5; 4:10
Intent is to use this key-only table for code to do code first strongly typed definitions while also being light weight, and it seemed like the most straight forward way to do it before running into problems.
Current code:
public class ImportFileDependency
{
[Key]
[ForeignKey("ImportFile")]
public int ImportFileId { get; set; }
[ForeignKey("Id")]
public ImportFile ImportFile {get; set;}
[Key]
[ForeignKey("ImportFile")]
public int ImportFileDependencyId { get; set; }
[ForeignKey("Id")]
public ICollection<ImportFile> ImportFileDependencies { get; set; }
}
public class ImportFile
{
[Key]
public int Id {get; set;}
public string Name { get; set; }
public string WorkbookTab { get; set; }
public string File { get; set; }
public ICollection<ImportFileDependency> Dependencies { get; set; }
}
...
modelBuilder
.Entity<ImportFileDependency>(e =>{
e.HasKey(ifd => new { ifd.ImportFileId, ifd.ImportFileDependencyId });
e.HasOne(ifd => ifd.ImportFile)
.WithMany(i => i.Dependencies);
});
modelBuilder
.Entity<ImportFile>()
.HasMany(i => i.Dependencies)
.WithOne()
.HasForeignKey(z => z.ImportFileId);
...
After multiple revisions of following the responses of the add-migration exception response, currently on:
There are multiple properties pointing to navigation 'ImportFile' in entity type 'ImportFileDependency'. To define composite foreign key using data annotations, use ForeignKeyAttribute on navigation.
which did not update from the most recent iteration.
I seem to have recursed into a deadend so looking for guidance
Given the time you've asked it, you probably found the answer yourself or gave up on it, but if someone else struggles with this error, this solved my issue: Entity Framework Code First - two Foreign Keys from same table
You have to define the relationship using fluent API.

2 Objects of same type mapping to MySQL DB problem

public class Purchase {
public Address to { get; set; }
public Address from { get; set; }
}
public class Address {
public string name { get; set; }
}
I have 1 Purchase with 2 Address. How this should be on the Database (mySql) including foreign keys to be used on Entity Framework.
I have the problem that entity understands (based on fk) that Navigability in Address is 1 to many (*) and I don't have a list of Address on Purchase, i have defined 2.
Thanks,
Bart.
You can configure relationships of the tables.Build your model like this,
public class PurchaseConfiguration : EntityTypeConfiguration<Purchase >
{
public PurchaseConfiguration()
{
HasRequired(p=>p.to ).WithOptionalDependent().WillCascadeOnDelete(false);
HasRequired(p => p.from ).WithOptionalDependent().WillCascadeOnDelete(false);
}
}
and your db context you can add the configurations like this,
public class yourDbContext:DbContext
{
public DbSet<Purchase> Purchases{ get; set; }
public DbSet<Address> Addresses{ get; set; }
//other db sets here..
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new PurchaseConfiguration ());
// you can add configurations for other tables
}
}

EF 4.1 CF Fluent API mapping problemo

I'm new to the fluent API. I have a legacy database which I can't alter at the moment. Simply, this is what I need to achieve:
public class ItemCategory
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
public class Item
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<ItemCategory> ItemCategories { get; set; }
public virtual ICollection<Item> RelatedItems { get; set; }
}
Items can be in many categories, RelatedItems can be in different categories to the current Item (which may not have any related items), the existing join tables look like this:
ItemCategoriesItems (ID,ItemCategoryID,ItemID)
RelatedItemCategoriesItems (ID,ItemCategoriesItemsID,RelatedItemCategoriesItemsID)
Hopefully it's obvious that the related items join table above contains 2 foreign keys to the item categories join table - one pointing to the current item and the other to the related item. Currently my onModelCreating code has:
modelBuilder.Entity<ItemCategory>()
.HasMany(c => c.Items)
.WithMany(set => set.ItemCategories)
.Map(mc =>
{
mc.ToTable("ItemCategoriesItems","testdb");
mc.MapLeftKey("ItemCategoryID");
mc.MapRightKey("ItemID");
});
... which gets the categories/items working but I'm stuck on how to get the RelatedItems.
Any help greatly appreciated!

EF 4 Code First and M2M2M

Using Entity Framework 4 and code first how would I create a model that supports this scenario:
In an application, there are users, each user belongs to one or more groups and for each group the user can have one or more roles.
Example:
I would like to be able to say, "give me Lisa", and the response returns a user object for lisa, with the groups she belongs to. For each group there is a list property with all the roles she has for that particular group
Can anyone help me model this using code first, any help/code samples, would be great!
/Best regards Vinblad
Edit: Here is new model for your requirement.
public class User
{
public virtual int Id { get; set; }
public virtual ICollection<UserPermission> Permissions { get; set; }
}
// Permission is extended junction table to model M:N between
// User and Group but in addition it contains relation to Roles.
// The ony disadvantage is that this model doesn't control that
// role in the collection is also the role related to group. You
// must either enforce it in application logic or create some additional
// database construct to check it.
public class UserPermission
{
public virtual int UserId { get; set; }
public virtual int GroupId { get; set; }
public virtual Group Group { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
public class Group
{
public virtual int Id { get; set; }
public virtual ICollection<UserPermission> UserPermissions { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
public class Role
{
public virtual int Id { get; set; }
public virtual ICollection<Group> Groups { get; set; }
public virtual ICollection<UserPermission> UserPermissions { get; set; }
}
public class Context : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Group> Groups { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<UserPermission> UserPermissions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Permission has composite key
modelBuilder.Entity<UserPermission>()
.HasKey(p => new {p.UserId, p.GroupId});
// Permission doesn't have navigation property to user
modelBuilder.Entity<User>()
.HasMany(u => u.Permissions)
.WithRequired()
.HasForeignKey(p => p.UserId);
modelBuilder.Entity<Group>()
.HasMany(g => g.UserPermissions)
.WithRequired(p => p.Group)
.HasForeignKey(p => p.GroupId);
}
}
As described in code there is small disadvantage. You can avoid the disadvantage by enforcing data integrity in DB by additional FK which can't be modeled by code first. You can use custom initializer to add that FK:
public class CustomInitializer : DropCreateDatabaseIfModelChanges<Context>
{
protected override void Seed(Context context)
{
context.Database.ExecuteSqlCommand(
#"ALTER TABLE [dbo].[RoleUserPermissions]
WITH CHECK ADD CONSTRAINT [FK_RoleUserPermissions_RoleGroups]
FOREIGN KEY([Role_Id], [UserPermission_GroupId])
REFERENCES [dbo].[RoleGroups] ([Role_Id], [Group_Id])");
}
}
Just add this to your application initialization (only for debug - application should not be able to drop its database in release):
Database.SetInitializer(new CustomInitializer());