ef code first mapping one table into some entities - c#

I try to solve following:
In the entities HRCard and BPCard I have properties
public int DefaultAddressId { get; set; }
[ForeignKey("DefaultAddressId")]
public AddressDetail Address { get; set; } // table AddressDetail
so far no problem, now my Problem:
In BPCard i have additionally a property:
public virtual ICollection<AddressDetail> Addresses { get; set; } //table AddressDetail
following the complete Code:
public abstract class EntityBase : IEntityModel {
[Key]
public int EntityId { get; set; }
[Required]
[StringLength(50)]
public string EntityKey { get; set; }
//...
}
// table HRCards
public class HRCard : EntityBase {
//Id from base class
// working fine
//...
public int DefaultAddressId { get; set; }
[ForeignKey("DefaultAddressId")]
public AddressDetail Address { get; set; } // table AddressDetail
}
// table BPCards
public class BPCard : EntityBase {
//Id from base class
// working fine
//...
public int DefaultAddressId { get; set; }
public int DefaultContactId { get; set; }
//working fine
[ForeignKey("DefaultAddressId")]
public AddressDetail DefaultAddress { get; set; } //table AddressDetail
//how can i solve this??
// table AddressDetail
public virtual ICollection<AddressDetail> Addresses { get; set; }
}
public class AddressDetail : EntityBase {
//Id from base class
// working fine
//...
public int ParentId { get; set; }
}
I have long time searched, but no result solve my problem really. My 1st solution was split the table into HRAddress and BPAddress this is working fine.
Edit:
If I start enable migrations I get an error message:
"The property 'ParentId' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection where T is a valid entity type."
many thanks
PS:
can I change the tags later for better mapping?

It depends on the expected relationship you want with the Address POCO
You can solve the relationship using the annotation with something like
public class AddressDetail : EntityBase {
//Id from base class
public virtual ICollection<BPCard> Addresses { get; set; }
//public virtual BPCard Addresses { get; set; }
public virtual ICollection<HRCard> Addresses { get; set; }
//public virtual BPCard Addresses { get; set; }
}
or directly on your model with something like
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//one-to-many
modelBuilder.Entity<HRCard>()
.HasMany<AddressDetails>(s => s.Id)
.WithRequired(s => s.HRCard)
.HasForeignKey(s => s.AddressId);
}

Related

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Users_Agencies_UserID"

im using entity framework core 5.0 and i created my one to many relationship with fluent api.
im getting that error when i try to create a new user in my project.
let me show u to my User class:
public class User : Base
{
[Key]
public int UserID { get; set; }
public string UserName { get; set; }
public string UserSurname { get; set; }
public string UserPassword { get; set; }
public string UserEMail { get; set; }
public int? AgencyID { get; set; }
public virtual Agency Agency { get; set; }
}
public class UserConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.HasKey(user => user.UserID);
}
}
and here its a Agency class which is related to User class:
public class Agency : Base
{
[Key]
public int AgencyID { get; set; }
public string AgencyName { get; set; }
public string AgencyPhoto { get; set; }
public string AgencyEMail { get; set; }
public string AgencyPhone { get; set; }
public int AgencyExportArea { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public class AgencyConfiguration : IEntityTypeConfiguration<Agency>
{
public void Configure(EntityTypeBuilder<Agency> builder)
{
builder.HasKey(agency => agency.AgencyID);
builder.HasMany(us => us.Users)
.WithOne(us => us.Agency)
.HasForeignKey(au => au.UserID)
.IsRequired(false)
}
}
i know,im getting that error SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Users_Agencies_UserID". The conflict occurred in database "anan", table "dbo.Agencies", column 'AgencyID'. because there is a no data in Agency table. The thing which im trying to do is make that AgencyID foreign key optional as a nullable. in User class u can see i defined that AgencyID as a nullable.
do i really need to define that relationship as a one-to-one or zero or is there a another way to do that ?
if i have to define that relationship as a one-to-one or zero,can u show me the way how can i do that.
Since you are using EF core 5 you don't need:
public class UserConfiguration : IEntityTypeConfiguration<User>
and
public class AgencyConfiguration : IEntityTypeConfiguration<Agency>
All this code is reduntant. You have a standart one-to-many relation that EF core recognizes and configures by default. Remove all of this code and everything will be fine.
But if you are a student and need to do everything hard way, you can add this reduntant code:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>(entity =>
{
entity.HasOne(d => d.Agency)
.WithMany(p => p.Users)
.HasForeignKey(d => d.AgencyId)
.OnDelete(DeleteBehavior.ClientSetNull);
});
}
And since you are interested in a configuration, these are another redundant attributes:
public class User : Base
{
[Key]
public int UserID { get; set; }
.....
public int? AgencyID { get; set; }
[ForeignKey(nameof(AgencyId))]
[InverseProperty("Users")]
public virtual Agency Agency { get; set; }
}
public class Agency : Base
{
[Key]
public int AgencyID { get; set; }
.....
[InverseProperty(nameof(User.Agency))]
public virtual ICollection<User> Users { get; set; }
}

EntityHistory fails for owned type with reference to other entities

Here is my model:
public class Investment : FullAuditedEntity<Guid>
{
public string some_property { get; set; }
public Address Address { get; set; }
}
public class Address : ValueObject<Address>
{
[ForeignKey("CountryId")]
public Country Country { get; set; }
[Required]
public int CountryId { get; set; }
[ForeignKey("StateId")]
public State State { get; set; }
public string StateId { get; set; }
[ForeignKey("DistrictId")]
public District District { get; set; }
public string DistrictId { get; set; }
[ForeignKey("CommuneId")]
public Commune Commune { get; set; }
public string CommuneId { get; set; }
[Required]
public string City { get; set; }
public string Street { get; set; }
}
When I try to create new investment and save to DB, ABP tries to identify whether it should store entity changes in history tables, but it crashes when trying to identify owner (investment) for owned entity (address).
This is because ABP always take first foreignkey (assuming it's relation to owner entity), but in my case the first foreignkey is relationship to some other entity, thus there is no "PrincipalToDependent" value and save action is terminated:
Is there any workaround for this or we cannot have references stored in owned entity type?
In case anyone wanted a workaround, It's needed to overwrite default foreign key for owned entity, so that we pass property name that would be always at first position in the foreign keys collection:
public class InvestmentConfiguration : IEntityTypeConfiguration<Investment>
{
public void Configure(EntityTypeBuilder<Investment> configuration)
{
configuration.OwnsOne(typeof(Address), "Address", buildAction => buildAction.HasForeignKey("AInvestmentId"));
}
}
then in DBContext class:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new InvestmentConfiguration());
}
and the result is:

EF Core does not track entity correctly

Hi I have problem with EF Core insert entity. The problem is that I need to insert new entity with relation to another one which is already existing. I have created the relations with fluent API. I have done this for two times. First I am creating car and adding the last edited by field with Identity user and all works but when I am trying to do the same with another entity it crashes down with
My fluent APi code which works good:
builder.Entity<Car>()
.HasOne(x => x.Owner)
.WithMany(x => x.OwnerCars)
.HasForeignKey(x => x.OwnerId);
Here is car entity:
public class Car : CarBase
{
[Key]
public int CarId { get; set; }
public bool IsTrailer { get; set; }
public virtual TrailerType TrailerType { get; set; }
public virtual int? TrailerTypeId { get; set; }
public virtual ApplicationUser Owner { get; set; }
public virtual string OwnerId { get; set; }
}
and here is Application user entity
public class ApplicationUser : IdentityUser
{
[MaxLength(100)]
public string Address { get; set; }
public DateTime CreatedDateTime { get; set; }
public DateTime LastEditationDateTime { get; set; }
public virtual ApplicationUser LastEditedBy { get; set; }
public bool IsDeleted { get; set; }
public virtual DateTime DeletedDateTime { get; set; }
public ICollection<DriverLicenseApplicationUser> DriverLicenses { get; set; }
public ICollection<RideApplicationUser> Rides { get; set; }
public ICollection<Car> OwnerCars { get; set; }
public ICollection<Car> EditedCars { get; set; }
public ICollection<Trailer> EditedTrailers { get; set; }
public ICollection<Customer> EditedCustomers { get; set; }
}
To add this entity I only call this function and all works.
public Car CreateCar(Car car)
{
_context.Cars.Add(car);
return car;
}
But when I want to save this way this another entity type it shows an error. All steps are same so I do not understand this. Here I am adding the code I use to do that.
builder.Entity<Trailer>()
.HasOne(x => x.TrailerType)
.WithMany(x => x.Trailers)
.HasForeignKey(x => x.TrailerTypeId);
Here is Trailer:
public class Trailer : CarBase
{
[Key]
public int TrailerId { get; set; }
//[Required]
public virtual TrailerType TrailerType { get; set; }
public virtual int TrailerTypeId { get; set; }
}
and here is traylerTyper:
public class TrailerType:Trackable
{
//[Key]
public int TrailerTypeId { get; set; }
[MaxLength(100)]
[Required]
public string Type { get; set; }
public string Note { get; set; }
public ICollection<Car> TrailerTypeCars { get; set; }
public ICollection<Trailer> Trailers{ get; set; }
}
and the method is the same as the one already mentioned
public Trailer CreateTrailer(Trailer trailer)
{
trailer.TrailerTypeId = trailer.TrailerType.TrailerTypeId;
//_context.Attach(trailer.TrailerType);
var result = _context.Trailers.Add(trailer);
return result.Entity;
}
When I uncomment the attach it works but I think that I dont have to attach this because I have got the relation based on IDs and the example mentioned first works great. It gives me no sense. So if anyone could give me advice it would be awsome.
Here is the error I am getting:
Cannot insert explicit value for identity column in table 'TrailerTypes' when IDENTITY_INSERT is set to OFF.
It looks like the EF doesnt know that the traylertype entity already exists and is trying to insert the same entity again and the app crashes because it already exists and I am not allowing to insert IDs directly. As I said I have absolutely no idea why is this happening.
The problem is Lazy loading. Propetry from ViewModel is not completly same as property in Database and EF tracks whole graph of property in object and doesn´t recognize that it is the same object. The solution is to work only with IDs instead with whole objects.

Entity object property with a timestamp/rowversion field?

I am using entity framework 6 and i have a base entity called EntityBase, very simple as so
public abstract class EntityBase : IEntityBase
{
public virtual long Id { get; set; }
}
Each of my entities inherit from this. Now some entities need audit information, so i have first created an interface called IAudit
public interface IAudit
{
Audit Audit { get; set; }
}
And the Audit object like so
public class Audit
{
public bool IsDeleted { get; set; }
public DateTime? DeletedDate { get; set; }
public long? DeletedByUserId { get; set; }
public DateTime CreatedDate { get; set; }
public long CreatedByUserId { get; set; }
public DateTime UpdatedDate { get; set; }
public long UpdatedByUserId { get; set; }
public byte[] RowVersion { get; set; }
}
And if an entity needs audit information, i apply this interface. Here is an example
public class Attachment : EntityBase, IAudit
{
#region IAudit
public Audit Audit { get; set; }
#endregion
public string Name { get; set; }
}
I am using code first, so now in my DbContext, i have this in my OnModelCreating method
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Configurations.Add(new AttachmentConfig());
}
Here is my AttachmentConfig file, which inherits from EntityBaseConfig, both included below
public abstract class EntityBaseConfig<TEntity> : EntityTypeConfiguration<TEntity>
where TEntity : EntityBase
{
public EntityBaseConfig()
{
this.HasKey(e => e.Id);
}
}
class AttachmentConfig : EntityBaseConfig<Attachment>
{
public AttachmentConfig()
: base()
{
this.Property(e => e.Name)
.HasMaxLength(255)
.IsRequired();
}
}
Now when my table is created, the Attachment table has the columns i would expect, but Audit_RowVersion is varbinary(max) instead of timestamp.
I have tried to put this line in each config file but get this error.
this.Property(e => e.Audit.RowVersion).IsRowVersion();
Schema specified is not valid. Errors: (36,14) : error 2039: The
conceptual side property 'RowVersion' has already been mapped to a
storage property with type 'rowversion'. If the conceptual side
property is mapped to multiple properties in the storage model, make
sure that all the properties in the storage model have the same type.
But i do not want to write this line in each file anyway. How can i get the Audit.RowVersion column to generate as a timestamp, and ideally write it once, so that all objects that implement IAudit also get the configured fields?
EDIT:
I have now added a config file for the Audit object, which looks like this
public class AuditConfig : EntityTypeConfiguration<Audit>
{
public AuditConfig() : base()
{
this.Property(e => e.RowVersion)
.IsRowVersion();
}
}
And i call this in the OnModelCreating method, like so, before the other Configuration calls
modelBuilder.Configurations.Add(new AuditConfig());
Now when i run my project, i get the following error
A table can only have one timestamp column. Because table 'Attachment'
already has one, the column 'Audit_RowVersion' cannot be added.
If i look at the database and Attachment table created so far, it has the fields in the Audit object, but they do not have the Audit_ prefix? Maybe this is a clue to someone?
You can use DataAnnotations.
In your model class
public class Audit
{
public bool IsDeleted { get; set; }
public DateTime? DeletedDate { get; set; }
public long? DeletedByUserId { get; set; }
public DateTime CreatedDate { get; set; }
public long CreatedByUserId { get; set; }
public DateTime UpdatedDate { get; set; }
public long UpdatedByUserId { get; set; }
[TimeStamp]
public byte[] RowVersion { get; set; }
}
You can add [TimeStamp] Attribute for your variable.

Entity Framework Creates unwanted relationship between abstract and derived tables

Using code first, I have some abstract classes and some classes derived from those abstracted classes.
// Abstracted Classes
public abstract class Brand
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public abstract class Model
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
// Derived Classes
[Table("ComparisonBrand")]
public class ComparisonBrand : Brand
{
public ComparisonBrand()
{
ComparisonValues = new List<ComparisonValue>();
Models = new List<ComparisonModel>();
}
public virtual ICollection<ComparisonValue> ComparisonValues { get; set; }
public virtual ICollection<ComparisonModel> Models { get; set; }
}
[Table("ComparisonModel")]
public class ComparisonModel : Model
{
public int? BrandId { get; set; }
public int? LogoId { get; set; }
[ForeignKey("BrandId")]
public virtual ComparisonBrand ComparisonBrand { get; set; }
[ForeignKey("LogoId")]
public virtual ComparisonLogo ComparisonBrand { get; set; }
public virtual ICollection<ComparisonValue> ComparisonValues { get; set; }
}
My issue is that the migration generates foreign keys for:
ComparisonModel.Id > Models.Id
ComparisonModel.BrandId > Brands.Id
ComparisonModel.BrandId > ComparisonBrand.Id
Since ComparisonBrand.Id is a FK to Brands.BrandId, I get an error when deleting a Brand record. If I delete the ComparisonModel.BrandId > ComparisonBrand.Id relationship, however, the delete works fine.
How can I prevent a relationship from being formed between both the abstracted table and the derived table (Brands and ComparisonBrand)?
You are using the virtual keyword this causes Lazy Loading. You are telling EF to generate Foreign keys for them through this feature. Drop the virtual and you will not create the keys any longer

Categories

Resources