C
C#2y ago
AbrahamJLR

❔ Entity Framework relationship problem between entities.

Hello, I am currently doing an Entity Framework internship and I have a relationship problem between entities. In the beginning we are given a base class "Entity", which has certain properties, its diagram is clear and simple. I associate the original class:
public abstract class Entity
{
[Key]
public Int64 Id { get; set; }
public Boolean IsDeleted { get; set; } = false;

public String CreatedByUser { get; set; } = string.Empty;
public String EditedByUser { get; set; } = string.Empty;
public String DeletedByUser { get; set; } = string.Empty;

public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime EditedAt { get; set; } = DateTime.UtcNow;
public DateTime DeleteAt { get; set; } = DateTime.UtcNow;
}
public abstract class Entity
{
[Key]
public Int64 Id { get; set; }
public Boolean IsDeleted { get; set; } = false;

public String CreatedByUser { get; set; } = string.Empty;
public String EditedByUser { get; set; } = string.Empty;
public String DeletedByUser { get; set; } = string.Empty;

public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime EditedAt { get; set; } = DateTime.UtcNow;
public DateTime DeleteAt { get; set; } = DateTime.UtcNow;
}
This class is inherited by the "User" class. Code:
public class User : Entity
{
[Required, StringLength(50)]
public String Name { get; set; } = string.Empty;

[Required, StringLength(100)]
public String LastName { get; set; } = string.Empty;

[Required, EmailAddress]
public String EmailAddress { get; set; } = string.Empty;

[Required]
public String Password { get; set; } = string.Empty;
}
public class User : Entity
{
[Required, StringLength(50)]
public String Name { get; set; } = string.Empty;

[Required, StringLength(100)]
public String LastName { get; set; } = string.Empty;

[Required, EmailAddress]
public String EmailAddress { get; set; } = string.Empty;

[Required]
public String Password { get; set; } = string.Empty;
}
My problem is the following assignment: "Create the necessary relationships between "User" and "Entity", where instead of using "String" for the properties (CreatedByUser, EditedByUser, DeletedByUser) use an instance of the "User" class. I've been having trouble posing this assignment correctly mainly because of the inheritance present between User and Entity. What would be the correct way to approach this relationship?
15 Replies
phaseshift
phaseshift2y ago
looks like a poorly posed question to me. An abstract class with all concrete and public properties? So you cannot override anything, and you cannot choose what to expose when inheriting it
AbrahamJLR
AbrahamJLR2y ago
I don't know what to answer. There are more classes that will also inherit from the Entity class, the question itself doesn't seem clear to me either, and that's why I don't know how to set up the assignment correctly. Before asking here I looked for more answers from people doing the same course on github and no one did that part. Since I don't have direct support for doubts, I ended up here thinking that it was a lack of knowledge on my part. May I abuse and ask how you would do the implementations of the properties present in Entity? Totally ignoring the example I supplied, I'm curious how you would do it from scratch.
phaseshift
phaseshift2y ago
Entity is fine, apart from the default dates. But entity is fine, assuming that all specialisations don't need/want to do something weird like
where instead of using "String" for the properties (CreatedByUser, EditedByUser, DeletedByUser) use an instance of the "User" class.
Are you absolutely sure this is correct?
This class is inherited by the "User" class. Code:
?
AbrahamJLR
AbrahamJLR2y ago
Yep, before the assignment, inheritance was already being done that way. And it only asks to modify the mentioned methods.
phaseshift
phaseshift2y ago
The only thing that makes sense to me is:
public abstract class Entity
{
[Key]
public Int64 Id { get; set; }
public Boolean IsDeleted { get; set; } = false;

public User user {get;set;} // <<

public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime EditedAt { get; set; } = DateTime.UtcNow;
public DateTime DeleteAt { get; set; } = DateTime.UtcNow;
}
public abstract class Entity
{
[Key]
public Int64 Id { get; set; }
public Boolean IsDeleted { get; set; } = false;

public User user {get;set;} // <<

public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime EditedAt { get; set; } = DateTime.UtcNow;
public DateTime DeleteAt { get; set; } = DateTime.UtcNow;
}
or it literally just means change the property types
public User CreatedByUser { get; set; };
public User EditedByUser { get; set; };
public User DeletedByUser { get; set; };
public User CreatedByUser { get; set; };
public User EditedByUser { get; set; };
public User DeletedByUser { get; set; };
AbrahamJLR
AbrahamJLR2y ago
In this case Entity Framework will not work, I get the error: The entity type 'User' requires a primary key to be defined. And my doubt is born right there, what would be the correct way to establish that relationship?
phaseshift
phaseshift2y ago
it has primary key - public Int64 Id { get; set; } if EF doesnt detect that, then thats a bit crappy. Add it with the model builder.
AbrahamJLR
AbrahamJLR2y ago
I still have the same doubts, but thanks anyway for trying to help me.
ffmpeg -i me -f null -
also, ef doesn't work with abstract classes have to be creatable mmm wouldn't it be cyclic
phaseshift
phaseshift2y ago
what is cyclic?
ffmpeg -i me -f null -
because Entity has a User, but User is a Entity how do you create one without creating the other
phaseshift
phaseshift2y ago
you dont create entity, its abstract base class...
ffmpeg -i me -f null -
exactly, but ef doesn't know this you are asking it to write it in the db might as well create it when recovering it
phaseshift
phaseshift2y ago
eh?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.