Many-many relationships to self - EF 6 port to EF Core 3.1 - ef-core-3.1

I have an entity Company that has many-to-many relationships to itself. The idea is to be able to navigate to all associating and associated companies using these relationships.
The following EF 6 code should explain how the relationships were mapped in EF 6.
public class Company {
...
public virtual ICollection<Company> AssociatedFrom { get; set; } = new List<Company>();
public virtual ICollection<Company> AssociatedTo { get; set; } = new List<Company>();
...
}
And this is how relationships were configured
HasMany(company => company.AssociatedTo)
.WithMany(x => x.AssociatedFrom)
.Map(mc =>
{
mc.MapLeftKey("Company_1");
mc.MapRightKey("Company_2");
mc.ToTable("Companies_Companies");
});
Now, the question is how can I create the same relationships in EF Core 3.1, keeping the same data structure as there is already a lot of data in the database we don't want to lose.
Note: EF Core 3.1, doesn't seem to support many-to-many relationships in the way that EF 6 did for .NET Framework.

Related

Entity Framework Core code first many-to-many save with only foreign keys [duplicate]

This question already has an answer here:
What is the correct way to do many to many entity relation insert?
(1 answer)
Closed 11 months ago.
I have a many-to-many relationship established code-first that works, with thousands of fake records generated for an API. Now I'm trying to save a new record on one side of that relationship given only the ids of the other side, as the client is passing in an array of int ids.
I've found plenty of questions with problems and answers about saving many-to-many in general, but none specifically about doing so with just a list of foreign keys. Perhaps I'm simply using the wrong terminology?
I could grab all the records for those ids up front, but it seems very heavy to wait for a database query, assign those entities to the new entity, and then go to the database again to save, when all I really need is to establish a relationship with ids I already have.
For single relationships I would just add the foreign key as a separate property and set that instead of the foreign entity itself:
public int? CarId { get; set; }
[ForeignKey("CarId")]
public CarModel? Car { get; set; }
Is there perhaps a similar paradigm for many-to-many?
Entity setup:
public class ClownModel {
public int Id { get; set; }
public List<CarModel> Cars { get; set; }
}
public class CarModel {
public int Id { get; set; }
public List<ClownModel> Clowns { get; set; }
}
DB Context OnModelCreating:
builder.Entity<ClownModel>()
.HasMany(x => x.Cars)
.WithMan(x => x.Clows);
You can use a "stub entity" to add an existing Car to a new or existing Clown without fetching the Car. Eg
var newClown = new Clown();
var car = new Car() { Id = carId };
db.Entry(car).State = EntityState.Unchanged;
newClown.Cars.Add(car);
db.Set<Clown>().Add(newClown);
db.SaveChanges();
Or include the linking entity in your model, which you can do without adding a DbSet property or changing the Many-to-Many navigation properties.
eg
builder.Entity<Clown>()
.HasMany(x => x.Cars)
.WithMany(x => x.Clowns)
.UsingEntity<ClownCar>(
c => c.HasOne(x => x.Car)
.WithMany()
.HasForeignKey(x => x.CarId),
c => c.HasOne(c => c.Clown)
.WithMany()
.HasForeignKey(c => c.ClownId)
);
then
var newClown = new Clown();
var clownCar = new ClownCar();
clownCar.CarId = carId;
clownCar.Clown = newClown;
db.Set<ClownCar>().Add(clownCar);
db.SaveChanges();

Self referencing / parent-child relationship one-to-zero or one in Entity Framework Core

I want to create a referencing / parent-child relationship one-to-zero or one in Entity Framework Core. I mean that my entity could have a parent:
public class MyEntity
{
public Guid Id { get; set; }
public Guid? ParentEntityId { get; set; }
public MyEntity ParentEntity { get; set; }
public MyEntity ChildEntity { get; set; }
}
I am trying to configure it via fluent api:
entity.HasOne(x => x.ParentEntity)
.WithOne(x => x.ChildEntity)
.HasForeignKey( .... )
I do not understand what I do have to write in the last line. I am not either sure my entity is correct.
Can anyone help me please?
EDIT: This question does not resolve my problem: Self referencing / parent-child relationship in Entity Framework
My problem is about create the foreign key. This line does not work:
.HasForeignKey(x => x.ParentEntityId)
HasForeignKey expects a string in input.
In a one-to-one relationship you always have to specify the dependent entity type in the HasForeignKey call, i.e. the entity that will contain the foreign key. For a one-to-one relationship between two different classes that makes sense, see the standard EF example. For a self-reference it looks obvious that EF should figure out there's no option. Still, you have to specify the type:
modelBuilder.Entity<MyEntity>()
.HasOne(x => x.ParentEntity)
.WithOne(x => x.ChildEntity)
.HasForeignKey<MyEntity>(c => c.ParentEntityId);

Unidirectional many-to-many relationship + Entity Framework Core + fluent api

I wanted to create unidirectional relation between Author and Skill.
My models are as below:
public class Author: Entity
{
...
public virtual ICollection<Skill> Skills { get; set; }
...
}
public class Skill : Entity
{
...
}
I want to configure it using EF Core's fluent API basic idea is not to create models based on the persistence layer's requirement.
My configuration is as below :
builder.HasMany(r => r.Skills).WithMany("NavigationNotRequired");

How do you get a web API odatamodelbuilder to work with EF fluent API mappings

I have created a fairly simply domain model using pocos. I have mapped these to an EF DB context using EntityTypeConfiguration<TEnitityType> classes. This all works fine.
I am now trying to create an OData V4 WebAPI controller endpoint using a ODataConventionModelBuilder and this is where things are coming unstuck. It all works fine until it encounters an association that is not convention based. But I cannot find a way to get the ODataBuilder to pick up the mappings from my EntityTypeConfiguration<TEnitityType> classes.
This leaves my with 2 unpalatable options
Decorate my lovely clean pocos with dirty attributes.
Re-map all the non convention based mappings manually using the ODataBuilder
Not sure if code samples will help but here they are anyway, i have simplified the entities for brevity.
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Item>("Items");
config.MapODataServiceRoute(
routeName: "odata",
routePrefix: "odata",
model: builder.GetEdmModel(),
batchHandler: new DefaultODataBatchHandler((GlobalConfiguration.DefaultServer)));
public class Item
{
public Int32 Id { get; set; }
public Int16 ItemTypeId { get; set; }
public virtual ItemType Type { get; set; }
public virtual ICollection<ItemVersion> Versions { get; set; }
public virtual ICollection<ItemTag> Tags { get; set; }
}
The problem comes when it encounters the ItemTags collection, here is an ItemTag:
public class ItemTag
{
public Int32 ItemId { get; set; }
public string Tag { get; set; }
public Item Item { get; set; }
}
Which you can see is not convention based and I have a configuration class for it as follows:
public class ItemTagConfiguration : EntityTypeConfiguration<ItemTag>
{
public ItemTagConfiguration()
{
HasKey(x => new {x.ItemId, x.Tag});
HasRequired(x => x.Item)
.WithMany(y => y.Tags)
.HasForeignKey(x => x.ItemId);
}
}
Does anyone know of a way that I can use these EntityTypeConfiguration files with an ODataBuilder or web API?
EDIT
If found this page which seems to indicate it might be possible with EF 6 which I am using. What I want to do is this
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Dbf>("Dbfs");
// modelBuilder.Configurations.Add(new DbfMap()); <---- NO GOOD - Needs Class from DBContext we only have a model builder :(
Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();
config.Routes.MapODataRoute("ODataRoute", "odata", model);
but the builder does not have a Configurations property.
Two things:
I have read multiple sources now that ward against using lazy loading and serialization; which is basically what OData is; (It even uses the system.runtime.serialization.datacontract and datamember attributes)
I have had more success in explicitly loading from context, and defining navigation properties in the modelbuilder for dbContext. I understand you are looking at customized nav properties, but I am fairly sure these are overriden methods useful for the ODataModelBuilder class (that does not assume much and needs less Entity Framework to work). Where you mentioned using EF already, I imagine that is the direction you will work, and if you do not need to alias your model names, you add an entry for each Set, using convention naming.
EntitySet("routePrefixName")
in building the EdmModel, and it wires up the relationships you made using Fluent previously. If you do have to add extraneous items to the underlying model, you should define each class as an EntityType<>(), only setting the key. EdmBuilder can use mild properties and key association to attach to the EF model in the ODataConventionModelBuilder.
I have wrestled and sought for some time, and there does not seem to be a wealth of information on .Net OData v4 floating around, probably due to the whole force datetimeoffset issue.
Hope that helps somewhat

EF 4.1 RC Many to many relationship in EF CF

I have two entities with many to many relationship like this:
class author
{
public int AuthorID{get;set;}
public string Name{get;set;}
public virtual ICollection<book> books{get;set;}
}
class book
{
public int BookID{get;set;}
public string Name{get;set;}
public virtual ICollection<author> authors{get;set;}
}
and I have an intermediate table named Bookauthor defined like this:
BookAuthor: int
int AuthorID
int BookID
How to map this using FluentAPI
Thanks!!
This was problematic with EDMX but with EF 4.1 fluent API you can map it:
modelBuilder.Entity<book>()
.HasMany(b => b.authors)
.WithMany(a => a.books)
.Map(m => m.MapLeftKey("BookID")
.MapRightKey("AuthorID")
.ToTable("BookAuthor"));
As you can see I don't map BookAuthor column. That column is unknown to EF and must be auto incremented.
This obviously can't work with a Code-first approach but only if you use Fluent API against existing database.
I don't think EF allows you to have a separate Id in many-to-many junction tables.
The alternative is just mapping BookAuthor as an entity.
On a side note, NHibernate supports this construct using an idbag