1

Is there an easy way to have a setup like this in EF Core?

ProjectEntity
Id
Name
List<Notes>

CustomerEntity
Id
Name
List<Notes>

NotesEntity
Id
Date
Note

Every parent entity would have a one-to-many relation to same child entity. So I can not use normal behavior as

NotesEntity
Id
ParentId
Date
Note

I have some idea to have like above but also add one field that said what the parent entity is, is that the right way to do it or is there a better way? If I use this way I can't use EF Core normal behavior with one-to-many relationship? I need to make more manual work for search / add and so on?

Edit : Entity Framework multiple parent tables I found this solution, but there I need to make a connection from my child to every parent I use, it could be alot of them.

Did also find a solution like :

BaseEntity
List<Notes>

ProjectEntity:BaseEntity

NotesEntity
Id
BaseEntityId
...

This last solution maybe is the best way to do it if I have alot of parent entities?

[EDIT 220922] Could [Owned] type has collection of other Items? Or this feature won't work on owned entitys? I guess this behavior isn't supported?

[Owned]
public class Note
{
    public int Id { get; set; }
    public string Text { get; set; }
    public int UserId { get; set; }
    public User User { get; set; }
    public ICollection<string> Tags { get; set; } 
}

I got an error on ICollection-row when I try to add-migration.

Unabel to determine the relationshop represented by navigation ... of typ 'ICollection' Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute.....

Maybe I could have one middleentity like :

public class NoteTagsEntity
{ 
    public int Id { get; set; }
    public ICollection<string> Tags { get; set; }
}

And then :

[Owned]
public class Note
{
    public int Id { get; set; }
    public string Text { get; set; }
    public int UserId { get; set; }
    public User User { get; set; }
    public int NoteTagsId { get; set; } 
    public NoteTagsId NoteTagsId { get; set; }
}

Edit I solved the Note functionality with having more FK's, one that point to Id of parent and one FK Id that point to what module that use that particular note. Here I don't have parent - child relation in my entities, I need to do this connection by myself but in this way it's easy to apply more modules that use note's later.

1 Answers1

0

Use Owned Entity Types, and each entity will get its own notes table.

eg

public abstract class Entity
{
    public int Id { get; set; }
}
public abstract class EntityWithNotes: Entity
{
   
    public virtual ICollection<Note> Notes { get; set; }
}
[Owned]
public class Note
{
    public int Id { get; set; }
    public string Text { get; set; }
}
public class Project : EntityWithNotes 
{
    public string Name { get; set; }
}

public class Customer : EntityWithNotes
{
    public string Name { get; set; }
}

creates

      CREATE TABLE [Customer_Notes] (
          [Id] int NOT NULL IDENTITY,
          [CustomerId] int NOT NULL,
          [Text] nvarchar(max) NOT NULL,
          CONSTRAINT [PK_Customer_Notes] PRIMARY KEY ([CustomerId], [Id]),
          CONSTRAINT [FK_Customer_Notes_Customer_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [Customer] ([Id]) ON DELETE CASCADE
      );

      CREATE TABLE [Project_Notes] (
          [Id] int NOT NULL IDENTITY,
          [ProjectId] int NOT NULL,
          [Text] nvarchar(max) NOT NULL,
          CONSTRAINT [PK_Project_Notes] PRIMARY KEY ([ProjectId], [Id]),
          CONSTRAINT [FK_Project_Notes_Project_ProjectId] FOREIGN KEY ([ProjectId]) REFERENCES [Project] ([Id]) ON DELETE CASCADE
      );
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
  • Thanks, I haven't used owned types before so I totally forgotten that this excists. I will give it a try! I call this notes entity from my reporitory of parent entity? If I want to get all notes to a specific parent I can do List nList = _db.parent.Where(i => i.Id == id).Collection(c => c.Notes).ToList() ? – Tobias Gustafsson Sep 08 '22 at 05:35