I'm about to create a Generic Entity and EntityTypeConfiguration for my entities. here are my classes:
IEntity
public interface IEntity<T>
{
T Id { get; set; }
}
public interface IAuditableEntity<T>
{
DateTime CreatedAt { get; set; }
Membership.User CreatedBy { get; set; }
int? CreatedById { get; set; }
DateTime? DeletedAt { get; set; }
Membership.User DeletedBy { get; set; }
int? DeletedById { get; set; }
T RevisionParentId { get; set; }
bool isLastVersion { get; set; }
}
Entity.cs
public abstract class BaseEntity
{
}
public abstract class Entity<T> : BaseEntity, IEntity<T>
{
public virtual T Id { get; set; }
}
public abstract class AuditableEntity<T> : Entity<T>, IAuditableEntity<T>
{
public DateTime CreatedAt { get; set; }
public virtual Membership.User CreatedBy { get; set; }
public int? CreatedById { get; set; }
public DateTime? DeletedAt { get; set; }
public virtual Membership.User DeletedBy { get; set; }
public int? DeletedById { get; set; }
public T RevisionParentId { get; set; }
public bool isLastVersion { get; set; }
}
The problem is when I try to define a generic EntityTypeConfiguration of AuditableEntity, because :
public class AuditableEntityConfig<T> : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<T> where T : AuditableEntity<int>
{
public AuditableEntityConfig()
{
HasOptional(x => x.CreatedBy).WithMany().HasForeignKey(x => x.CreatedById);
HasOptional(x => x.DeletedBy).WithMany().HasForeignKey(x => x.DeletedById);
Property(x => x.DeletedAt).IsOptional();
Property(x => x.RevisionParentId).IsOptional();
}
}
public class User : AuditableEntity<long>
{
}
You see I had to AuditableEntity<int> which is wrong and I have no idea what to replace to get it work.
AuditableEntity<int> should be something like AuditableEntity<T> and T can be string, int, guid, long, ...
UPDATE
as suggested by Mike answer, I made changes and updated my question:
public class User : AuditableEntity<int>
{
[Index("IX_uniqueUsername", IsUnique = true)]
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public bool Active { get; set; }
public virtual List<Role> Roles { get; set; }
public virtual List<UserGroup> Groups { get; set; }
public virtual UserProfile Profile { get; set; }
public bool isSuperAdmin { get; set; }
}
public class UserConfig : AuditableEntityConfig<User, int>
{
public UserConfig()
{
ToTable("Account_Users");
Property(x => x.Username).HasMaxLength(200).IsRequired();
Property(x => x.Password).HasMaxLength(200).IsRequired();
Property(x => x.Email).HasMaxLength(200);
HasMany(x => x.Roles).WithMany(x => x.Users).Map(x =>
{
x.ToTable("Account_UserRoles");
x.MapLeftKey("UserId");
x.MapRightKey("RoleId");
});
HasMany(x => x.Groups).WithMany(x => x.Users).Map(x =>
{
x.ToTable("Account_UserGroups");
x.MapLeftKey("UserId");
x.MapRightKey("GroupId");
});
}
}
I get this error now for the RevisionParentId property:
The type 'TK' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Data.Entity.ModelConfiguration.Configuration.StructuralTypeConfiguration<TStructuralType>.Property<T>(System.Linq.Expressions.Expression<System.Func<TStructuralType,T>>)
in this line:
Property(x => x.RevisionParentId).IsOptional();
where T : AuditableEntity<T> will cause recursive type checking. Please try
UPDATED
public class AuditableEntityConfig<T, TK> : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<T>
where T : AuditableEntity<TK> where TK : struct { }
Related
I have 4 tables
public class StokGroup:BaseModel
{
public string GroupName { get; set; }
public List<GroupFeature> GroupFeatures { get; set; }
public List<StokCard> StokCards { get; set; }
}
public class GroupFeature : BaseModel
{
public int StokGroupId { get; set; }
public StokGroup StokGroup { get; set; }
public string FeatureName { get; set; }
public List<StokCardAdditionalFeatureValue> StokCardAdditionalFeatureValues { get; set; }
}
public class StokCard : BaseModel
{
public string IPMCode { get; set; }
public int StokGroupId { get; set; }
public StokGroup StokGroup { get; set; }
.......................................
}
public class StokCardAdditionalFeatureValue:BaseModel
{
public int StokCardId { get; set; }
public StokCard StokCard { get; set; }
public int GroupFeatureId { get; set; }
public GroupFeature GroupFeature { get; set; }
public string Value { get; set; }
}
And my BaseModel is the class that has Primary Key ID all classes inherit from BaseModel
public class BaseModel
{
[Key]
public int Id { get; set; }
}
And I have also :
modelBuilder.Entity<StokCard>()
.HasOne(o => o.StokGroup)
.WithMany(m => m.StokCards)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<GroupFeature>()
.HasOne(o => o.StokGroup)
.WithMany(m => m.GroupFeatures);
modelBuilder.Entity<StokCardAdditionalFeatureValue>()
.HasOne(o => o.StokCard)
.WithMany(m => m.StokCardAdditionalFeatureValues)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<StokCardAdditionalFeatureValue>()
.HasOne(o => o.GroupFeature)
.WithMany(m => m.StokCardAdditionalFeatureValues);
Now the problem is when the stokgroup is deleted StokCard is deleted automatically because of the relationship
How can I achieve this problem ?
I set NULL to StokGroupId on StokCards table but migration FAILED
Would you mind please help ?
Thanks in advance
I searched a lot but no luck to find a solution to my entities.
I have 2 entities. Device & PersonEnterExit
I have 2 Collection of PersonEnterExit in Device entity.
When I run database update it says me you have same entity with different name. do manually name them. how do i do that ? I want to do this with fluent api in onModelCreating.
Devices.cs
using System;
using System.Collections.Generic;
namespace DesTech.Core.Entities
{
public class Device : BaseEntity
{
public bool? IsConnected { get; set; }
public string Name { get; set; }
public string Ip { get; set; }
public int Port { get; set; }
public DateTime? LastConnectTime { get; set; }
public DateTime? ChangeClockBatteryWarningTime { get; set; }
public bool HasFp { get; set; }
public virtual Location Location { get; set; }
public virtual ICollection<DeviceOptLog> DeviceOptLog { get; set; }
public virtual ICollection<DevicePerson> DevicePerson { get; set; }
public virtual ICollection<DeviceTimeZone> DeviceTimeZone { get; set; }
public virtual ICollection<PersonEnterExit> PersonEnterExitEnDevice { get; set; }
public virtual ICollection<PersonEnterExit> PersonEnterExitExDevice { get; set; }
public virtual ICollection<PersonPassLog> PersonPassLog { get; set; }
}
}
PersonEnterExit.cs
using System;
namespace DesTech.Core.Entities
{
public class PersonEnterExit : BaseEntity
{
public int Type { get; set; }
public int? PersonId { get; set; }
public DateTime? NoEnterTime { get; set; }
public int? EnDeviceId { get; set; }
public DateTime? EnVerifyDate { get; set; }
public string EnPlate { get; set; }
public int? ExDeviceId { get; set; }
public DateTime? ExVerifyDate { get; set; }
public virtual Device EnDevice { get; set; }
public virtual Device ExDevice { get; set; }
public virtual Person Person { get; set; }
}
}
You can use one of these
FluentApi
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Device>()
.HasMany(a => a.PersonEnterExitEnDevice)
.WithOne(a => a.EnDevice)
.HasForeignKey(a => a.EnDeviceId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Device>()
.HasMany(a => a.PersonEnterExitExDevice)
.WithOne(a => a.ExDevice)
.HasForeignKey(a => a.ExDeviceId).OnDelete(DeleteBehavior.Restrict);
}
Metadata. you can use InverseProperty to define the relationships.
namespace DesTech.Core.Entities
{
public class PersonEnterExit : BaseEntity
{
public int Type { get; set; }
public int? PersonId { get; set; }
public DateTime? NoEnterTime { get; set; }
public int? EnDeviceId { get; set; }
public DateTime? EnVerifyDate { get; set; }
public string EnPlate { get; set; }
public int? ExDeviceId { get; set; }
public DateTime? ExVerifyDate { get; set; }
[InverseProperty("PersonEnterExitEnDevice")]
public virtual Device EnDevice { get; set; }
[InverseProperty("PersonEnterExitExDevice")]
public virtual Device ExDevice { get; set; }
public virtual Person Person { get; set; }
}
}
namespace DesTech.Core.Entities
{
public class Device : BaseEntity
{
public bool? IsConnected { get; set; }
public string Name { get; set; }
public string Ip { get; set; }
public int Port { get; set; }
public DateTime? LastConnectTime { get; set; }
public DateTime? ChangeClockBatteryWarningTime { get; set; }
public bool HasFp { get; set; }
public virtual Location Location { get; set; }
public virtual ICollection<DeviceOptLog> DeviceOptLog { get; set; }
public virtual ICollection<DevicePerson> DevicePerson { get; set; }
public virtual ICollection<DeviceTimeZone> DeviceTimeZone { get; set; }
[InverseProperty("EnDevice")]
public virtual ICollection<PersonEnterExit> PersonEnterExitEnDevice { get; set; }
[InverseProperty("ExDevice")]
public virtual ICollection<PersonEnterExit> PersonEnterExitExDevice { get; set; }
public virtual ICollection<PersonPassLog> PersonPassLog { get; set; }
}
}
Like you mentioned, you need to explicitly configure both relationships. Otherwise, EF will get confused.
build.Entity<PersonEnterExit>()
.HasOne(e => e.EnDevice)
.WithMany(d => d.PersonEnterExitEnDevice);
build.Entity<PersonEnterExit>()
.HasOne(e => e.ExDevice)
.WithMany(d => d.PersonEnterExitExDevice);
See if this helps. You may need to explicitly configure foreign keys if this doesn't work.
Okey I combined two answers and its works now. I reply my question for maybe others use same approach. If i mistake please warn me. But it works now.
builder.HasMany(a => a.PersonEnterExitEnDevice)
.WithOne(a => a.EnDevice)
.HasForeignKey(a => a.EnDeviceId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasMany(a => a.PersonEnterExitExDevice)
.WithOne(a => a.ExDevice)
.HasForeignKey(a => a.ExDeviceId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(d => d.EnDevice)
.WithMany(e => e.PersonEnterExitEnDevice)
.HasForeignKey(d => d.EnDeviceId)
.HasConstraintName("FK_PersonEnterExit_DeviceEn");
builder.HasOne(d => d.EnLocation)
.WithMany(e => e.PersonEnterExitEnLocation)
.HasForeignKey(d => d.EnLocationId)
.HasConstraintName("FK_PersonEnterExit_LocationEx");
I'm trying to make a simple app to try Entity Framework Core, but i a have problem with setting up relations between entities. My entities:
public class Card
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Adress { get; set; }
public DateTime DoB { get; set; }
public DateTime DoS { get; set; }
public User Portal { get; set; }
public List<Reservation> Res { get; set; }
}
public class Doctor
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public TimeSpan Start_Working { get; set; }
public TimeSpan End_Working { get; set; }
public List<Reservation> Reservations { get; set; }
public int SpecID { get; set; }
public Spec Spec { get; set; }
}
public class Reservation
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime DoR { get; set; }
public string Info { get; set; }
public int CardID { get; set; }
public Card Card_Nav_R { get; set; }
public int DoctorID { get; set; }
public Doctor Doctor { get; set; }
}
public class Spec
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public List<Doctor> Doctors { get; set; }
}
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public int CardID { get; set; }
public Card Card { get; set; }
}
And a configuration class where i tried to set up relations:
class ApplicationContext:DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Card> Cards { get; set; }
public DbSet<Reservation> Reservations { get; set; }
public DbSet<Doctor> Doctors { get; set; }
public DbSet<Spec> Specs { get; set; }
public ApplicationContext()
{
Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder ModelBuilder)
{
ModelBuilder.Entity<User>().HasKey(u => u.Id);
ModelBuilder.Entity<Card>().HasKey(c => c.Id);
ModelBuilder.Entity<Doctor>().HasKey(d => d.Id);
ModelBuilder.Entity<Spec>().HasKey(s => s.Id);
ModelBuilder.Entity<Reservation>().HasKey(r => r.Id);
ModelBuilder.Entity<User>().Property(u => u.Email).IsRequired();
ModelBuilder.Entity<User>().Property(u => u.Password).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.Name).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.Surname).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.DoB).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.Adress).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Name).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Surname).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Spec).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Email).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Start_Working).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.End_Working).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.Info).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.Card_Nav_R).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.Doctor).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.DoR).IsRequired();
ModelBuilder.Entity<Spec>().Property(s => s.Name).IsRequired();
ModelBuilder.Entity<Doctor>().HasOne<Spec>(d=>d.Spec).WithMany(s => s.Doctors).HasForeignKey(d => d.SpecID);
ModelBuilder.Entity<User>().HasOne<Card>(u => u.Card).WithOne(c => c.Portal).HasForeignKey<User>(u => u.CardID);
ModelBuilder.Entity<Reservation>().HasOne<Card>(r => r.Card_Nav_R).WithMany(c => c.Res).HasForeignKey(r => r.CardID);
ModelBuilder.Entity<Reservation>().HasOne<Doctor>(r => r.Doctor).WithMany(d => d.Reservations).HasForeignKey(r => r.DoctorID);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Simple_Try;Trusted_Connection=True;");
}
}
So, when i tried to add migration or add something to database i saw this error:
System.InvalidOperationException: 'The property or navigation 'Spec' cannot be added to the entity type 'Doctor' because a property or navigation with the same name already exists on entity type 'Doctor'.'
I really don't know how to fix this, i tried to use annotations instead of Fluent API, but had the same result.
The cause of the exception is the following line:
ModelBuilder.Entity<Doctor>().Property(d => d.Spec).IsRequired();
because Doctor.Spec is a navigation property
public class Doctor
{
// ...
public Spec Spec { get; set; }
}
and navigation properties cannot be configured via Property fluent API.
So simply remove that line. Whether reference navigation property is required or optional is controlled via relationship configuration. In this case
ModelBuilder.Entity<Doctor>()
.HasOne(d => d.Spec)
.WithMany(s => s.Doctors)
.HasForeignKey(d => d.SpecID)
.IsRequired(); // <--
although the IsRequired is automatically derived from the FK property type - since SpecID is non nullable, then the relationship is required.
For more info, see Required and Optional Properties and Required and Optional Relationships documentation topics.
I have the following Model
public class FilanthropyEvent : EntityBase, IDeleteable
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime EventDate { get; set; }
public string Description { get; set; }
public decimal Target { get; set; }
public decimal EntryFee { get; set; }
public bool Deleted { get; set; }
public ICollection<EventAttendee> EventAttendees { get; set; }
}
public class Attendee : EntityBase, IDeleteable
{
public int Id { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool MailingList { get; set; }
public bool Deleted { get; set; }
public ICollection<EventAttendee> EventAttendees { get; set; }
}
Events and Attendees is a many to many relationship but I needed another property on the association so I created an association entity
public class EventAttendee : EntityBase
{
public int FilanthropyEventId { get; set; }
public int AttendeeId { get; set; }
public bool InActive { get; set; }
public virtual Attendee Attendee { get; set; }
public virtual FilanthropyEvent FilanthropyEvent { get; set; }
}
These are the configurations for each FilanthropyEvent and Attendee
public class FilanthropyEventConfiguration : EntityTypeConfiguration<FilanthropyEvent>
{
public FilanthropyEventConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(x => x.EventAttendees).WithRequired(x => x.FilanthropyEvent).HasForeignKey(x => x.FilanthropyEvent);
}
}
public AttendeeConfiguration()
{
HasKey(x => x.Id);
Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
HasMany(x => x.EventAttendees).WithRequired(x => x.Attendee).HasForeignKey(x => x.AttendeeId);
}
public class EventAttendeesConfiguration : EntityTypeConfiguration<EventAttendee>
{
public EventAttendeesConfiguration()
{
HasKey(x => new {x.FilanthropyEventId, x.AttendeeId});
}
}
When I try and initialise the database via the update-database command in the package manager console I get the following error.
System.InvalidOperationException: The foreign key component 'FilanthropyEvent' is not a declared property on type 'EventAttendee'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.
I realise I'm probably missing a mapping in the EventAttendeesConfiguration class but what would be the correct mapping to model this relationship?
This code
HasMany(x => x.EventAttendees)
.WithRequired(x => x.FilanthropyEvent)
.HasForeignKey(x => x.FilanthropyEvent);
Should be
HasMany(x => x.EventAttendees)
.WithRequired(x => x.FilanthropyEvent)
.HasForeignKey(x => x.FilanthropyEventId);
I'm using CodeFirst for my devemopment. For all model classes in my Entity I have a base class named CommonFields
public class CommonFields
{
public int Status { get; set; }
public DateTime CreatedOn { get; set; }
public int CreaedBy { get; set; }
public DateTime ModifiedOn { get; set; }
public int ModifiedBy { get; set; }
}
And, for eg. I have two classes like
public class Employee : CommonFields
{
public int Id { get; set; }
public string Name { get; set; }
//Other properties
}
public class User : CommonFields
{
public int Id { get; set; }
public string Name { get; set; }
//Other properties
}
How can I set relation from CreatedBy & ModifiedBy to User table. I just need only one directional mapping.
I need to get User information when I write objEmployee.CreatedUser
Thanks.
public class CommonFields
{
public int Status { get; set; }
public DateTime CreatedOn { get; set; }
public int? CreatedById { get; set; }
public virtual User CreatedBy { get; set; }
public DateTime ModifiedOn { get; set; }
public virtual User ModifiedBy { get; set; }
public int? ModifiedById { get; set; }
}
and you have to define relations for all derived entities in your DbContext
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<User>().HasOptional(x => x.CreatedBy).WithMany().HasForeignKey(x => x.CreatedById).WillCascadeOnDelete(false);
mb.Entity<User>().HasOptional(x => x.ModifiedBy).WithMany().HasForeignKey(x => x.ModifiedById).WillCascadeOnDelete(false);
mb.Entity<Employee>().HasOptional(x => x.CreatedBy).WithMany().HasForeignKey(x => x.CreatedById).WillCascadeOnDelete(false);
mb.Entity<Employee>().HasOptional(x => x.ModifiedBy).WithMany().HasForeignKey(x => x.ModifiedById).WillCascadeOnDelete(false);
}
EDIT:
Or you could use TPH. Then your model creating look like this
protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Entity<CommonFields>().
.Map(x => x.ToTable("Users"))
.Map<User>(x => x.Requires("__type").HasValue(1)
.Map<Employee>(x => x.Requires("__type").HasValue(2);
mb.Entity<CommonFields>().HasOptional(x => x.CreatedBy).WithMany().HasForeignKey(x => x.CreatedById).WillCascadeOnDelete(false);
mb.Entity<CommonFields>().HasOptional(x => x.ModifiedBy).WithMany().HasForeignKey(x => x.ModifiedById).WillCascadeOnDelete(false);
}