I'm trying to create two entities as below and add referential constraints to them using Fluent API.
The design it to enforce required Primary Contact with optional Secondary Contact with added requirement that Primary and Secondary may be referring to two different contacts in the ContactInfo entity.
public class Employee
{
public int EmployeeId { get; set; }
public int PrimaryContactId { get; set; }
public int SecondaryContactId { get; set; }
public virtual ContactInfo PrimaryContact { get; set; }
public virtual ContactInfo SecondaryContact { get; set; }
}
public class ContactInfo
{
public int ContactId { get; set; }
public string PhoneNumer { get; set; }
public string Email { get; set; }
}
public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
public EmployeeConfiguration ()
{
var employeeEntity = this;
employeeEntity.HasKey(e => e.EmployeeId).ToTable("Employees");
employeeEntity.Property(e => e.EmployeeId).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
employeeEntity.HasRequired(e => e.PrimaryContact).WithMany().HasForeignKey(e => e.PrimaryContactId).WillCascadeOnDelete(true);
employeeEntity.HasRequired(e => e.SecondaryContact ).WithMany().HasForeignKey(e => e.SecondaryContact Id).WillCascadeOnDelete(false);
}
}
It seems to create with required constraints as expected but when I try to add an employee with SecondaryContact set to null, the row created for this new Employee has SecondaryContactId set to same as PrimaryContactId which is not my intention.
I'm not able to understand whether the design is correct in the first place, or the configuration needs to be tweaked to get the right results.
Looks like you set both contacts to be required for your employee object.
If you want to have SecondaryContract optional change:
employeeEntity
.HasRequired(e => e.SecondaryContact)
.WithMany()
.HasForeignKey(e => e.SecondaryContactId)
.WillCascadeOnDelete(false);
to
employeeEntity
.HasOptional(e => e.SecondaryContact)
.WithMany()
.HasForeignKey(e => e.SecondaryContactId)
.WillCascadeOnDelete(false);
Addtionally change:
public int SecondaryContactId { get; set; }
to
public int? SecondaryContactId { get; set; }
since your SecondaryContactId is optional.
You can add foreign Key annotations and also set the keys to be nullable if you expect either to be empty.
public class Employee
{
public int EmployeeId { get; set; }
public int? PrimaryContactId { get; set; }
public int? SecondaryContactId { get; set; }
[ForeignKey("PrimaryContactId")]
public virtual ContactInfo PrimaryContact { get; set; }
[ForeignKey("SecondaryContactId")]
public virtual ContactInfo SecondaryContact { get; set; }
}
Make the secondary contact id to be a nullable type.
public int? SecondaryContactId { get; set; }
Related
I get on creating Migration some Warnings like this one:
The foreign key property 'AppUserClaim.UserId1' was created in shadow state because a conflicting property with the simple name 'UserId' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type. See https://aka.ms/efcore-relationships for information on mapping relationships in EF Core.
It applies to all entities with AppUser navigation property. Other navigation properties has no warning.
public class AppUser : IdentityUser<Guid>, IChangeTrackerObject
{
public string FirstName { get; set; }
public string LastName { get; set; }
[Column(TypeName = "text")]
public string ProfilePictureDataUrl { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public string ChangedBy { get; set; }
public DateTime? ChangedOn { get; set; }
public bool IsDeleted { get; set; }
public DateTime? DeletedOn { get; set; }
public bool IsActive { get; set; }
public string RefreshToken { get; set; }
public DateTime RefreshTokenExpiryTime { get; set; }
public virtual ICollection<AppUserClaim> Claims { get; set; }
public virtual ICollection<AppUserLogin> Logins { get; set; }
public virtual ICollection<AppUserToken> Tokens { get; set; }
public virtual ICollection<AppUserRole> UserRoles { get; set; }
public AppUser()
{
}
}
public class AppUserClaim : IdentityUserClaim<Guid>, IChangeTrackerObject
{
public string ChangedBy { get; set; }
public DateTime? ChangedOn { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public virtual AppUser User { get; set; }
}
private static void BuildIdentity(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AppUser>(entity =>
{
entity.ToTable(name: "Users", schema);
entity.Property(e => e.Id).ValueGeneratedOnAdd();
// Each User can have many UserClaims
entity.HasMany(e => e.Claims)
.WithOne()
.HasForeignKey(uc => uc.UserId)
.IsRequired();
// Each User can have many UserLogins
entity.HasMany(e => e.Logins)
.WithOne()
.HasForeignKey(ul => ul.UserId)
.IsRequired();
// Each User can have many UserTokens
entity.HasMany(e => e.Tokens)
.WithOne()
.HasForeignKey(ut => ut.UserId)
.IsRequired();
// Each User can have many entries in the UserRole join table
entity.HasMany(e => e.UserRoles)
.WithOne()
.HasForeignKey(ur => ur.UserId)
.IsRequired();
});
modelBuilder.Entity<AppUserClaim>(entity =>
{
entity.ToTable("UserClaims", schema);
});
}
I ran into a similar issue. In my OnModelCreating method, I had flipped the order in which I was applying migrations.
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
Basically, it seemed like if the call to the base method came after the call to apply the configurations from my code, then the configurations in the base method would override my configurations, which gave me a similar error to what you had. So what I'm assuming is happening is you're calling BuildIdentity() after you call base.OnModelCreating(). You may need to reverse that order, otherwise the relationships defined in the default identity DB may take precedence.
I want to have a one-to-many relation between subclasses of one master class,
and in second step use of master class for relationships with other class
please see below models:
BasePost.cs
public abstract class BasePost
{
[Key]
public long Id { get; set; }
public ICollection<Comment> Comments { get; set; }
}
Question.cs:
public class Question: BasePost
{
[Required] public string Title { get; set; }
public string Body { get; set; }
public ICollection<Answer> Answers { get; set; }
}
Answer.cs
public class Answer: BasePost
{
public string Body { get; set; }
public int Vote { get; set; }
public Question Question { get; set; }
public long QuestionId { get; set; }
}
Comment.cs
public class Comment
{
[Key]
public long Id { get; set; }
public string Text { get; set; }
public BasePost BasePost { get; set; }
public long BasePostId { get; set; }
public DateTime CreateDate { get; set; }
}
and finally in ApplicationDbContext.cs :
builder.Entity<Answer>()
.HasOne(x => x.Question)
.WithMany(a => a.Answers)
.HasForeignKey(k => k.QuestionId)
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<Comment>()
.HasOne(x => x.BasePost)
.WithMany(a => a.Comments)
.HasForeignKey(k => k.BasePostId)
.OnDelete(DeleteBehavior.ClientSetNull);
but after migration and update database I get below error:
Introducing FOREIGN KEY constraint 'FK_BasePosts_BasePosts_QuestionId' on table 'BasePosts' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
I have the following classes:
public class Bicycle
{
public int BicycleId { get; set; }
public DateTime YearOfManufacture { get; set; }
public int BicycleManufactuerId { get; set; }
public BicycleManufacturer BicycleManufacturer { get; set; }
}
public class BicycleManufacturer
{
public int BicycleManufacturerId { get; set; }
public string Description { get; set; }
}
Each Bicycle must have a BicycleManufacturer (1:1). There could be some BicycleManufacturer that isn't associate with any Bicycle. Most will be associated with multiple Bicycle entities.
I have the following fluent API code to set up the FK relationship:
modelBuilder.Entity<Bicycle>()
.HasRequired(a => a.BicycleManufacturer)
.WithMany()
.HasForeignKey(u => u.BicycleManufactuerId);
This all seems to work fine. However, I would really like to remove the BicycleManufacturerId property from the Bicycle entity. It's only there to establish the FK relationship. Is there a way I can create the proper FK relationship if I remove this property?
You can remove the property and use the mapping:
modelBuilder.Entity<Bicycle>()
.HasRequired(a => a.BicycleManufacturer)
.WithMany()
.Map(m => m.MapKey("BicycleManufactuerId"));
You can also do it by convention by adding the relationship on the other side as a collection.
public class Bicycle
{
public int BicycleId { get; set; }
public DateTime YearOfManufacture { get; set; }
public int BicycleManufactuerId { get; set; }
public BicycleManufacturer BicycleManufacturer { get; set; }
}
public class BicycleManufacturer
{
public int BicycleManufacturerId { get; set; }
public ICollection<Bicycle> Bicycles { get; set; }
public string Description { get; set; }
}
I have these two entities:
public partial class Suscriptores
{
public Suscriptores()
{
this.Publicacion = new HashSet<Publicacion>();
}
[Key]
public int IdSuscriptor { get; set; }
public string LogoSuscriptor { get; set; }
public string Identificacion { get; set; }
public string Nombre { get; set; }
public string Direccion { get; set; }
public string Telefono { get; set; }
public string Email { get; set; }
public string Fax { get; set; }
public string Home { get; set; }
public virtual ICollection<Publicacion> Publicacion { get; set; }
}
public partial class Publicacion
{
public Publicacion()
{
this.Edictos = new HashSet<Edictos>();
}
[Key]
public decimal IdPublicacion { get; set; }
public System.DateTime FechaPublicacion { get; set; }
public string IdUsuario { get; set; }
public System.DateTime FechaPublicacionHasta { get; set; }
public System.DateTime FechaArchivoHasta { get; set; }
public int IdSuscriptor { get; set; }
public decimal IdTipoPublicacion { get; set; }
[ForeignKey("IdSuscriptor")]
public virtual Suscriptores Suscriptores { get; set; }
}
When I try to run this query:
public ActionResult DetailsVSTO(string Identificacion)
{
var SusQ = from s in db.Suscriptores
where s.Identificacion == Identificacion
select s;
return Json(SusQ.First(), JsonRequestBehavior.AllowGet);
}
It throw this message:
System.Data.SqlClient.SqlException: Invalid column name 'Suscriptores_IdSuscriptor1'
Trying to solve this problem, I added this fluent API code at DBContext:
modelBuilder.Entity<Suscriptores>()
.HasMany(x => x.Publicacion)
.WithRequired(x => x.Suscriptores)
.Map(a => a.MapKey("IdSuscriptor"));
But the problem persists. How can I solve this?
Try add a many-to-one mapping as well. Please use pure Fluent API, and you should remove the [ForeignKey] annotations.
modelBuilder.Entity<Publicacion>()
.HasRequired(x => x.Suscriptores)
.WithMany(x => x.Publicacion);
I received this error in relation to a non-foreign key column and wasted far too much time trying to figure out the error. It was in my code, not in EF or the database. I had simply thought that I had typed
this.Property(t => t.Revision).HasColumnName("Revision");
this.Property(t => t.DistributionClass).HasColumnName("DistributionClass");
But what I had typed was
this.Property(t => t.Revision).HasColumnName("Revision");
this.Property(t => t.Revision).HasColumnName("DistributionClass");
I suppose I was looking at the line above and put t.Revision instead of t.DistributionClass. And no matter how long I looked at it I could not see my own mistake. With any luck this will save some other poor soul some time.
I had this issue in my Item table on a property (column) I had just added, and how frustrating!
Turns out I had a List property in the data model for Order, and because I did not Ignore it in that configuration it cause this same issue in the Item table. This would not have happened except that both tables had a property of the same name, so I had to do this... which I should have done anyways.
public OrderConfiguration() {
Ignore(p => p.Items);
}
Hello Guys In my case I had a legacy code with
two classes with different names of the same foreign key.
Adding the Annotation doing reference to the correct column and the name of attribute with the same name in other classes.then the annotation ForeignKey doing match between the both columns.
[Table("LFile")]
public partial class LFile
{
[Key]
public int FileId { get; set; }
[StringLength(500)]
public string FileName { get; set; }
public int? LRRFileDetailId { get; set; }
public byte[] FileData { get; set; }
public FileType Type { get; set; }
[Column("LUpload_Id")] //Foreign Key of the class
public int? LUploadId { get; set; }
[ForeignKey("LUploadId")] //Foreign key inherited
public virtual LParserProcess LParserProcess { get; set; }
}
I was getting the same error SqlException message where the number 1 was being appended to my field names.
I was able to solve it once I realized that I had incorrectly assumed the [ForeignKey] annotations refer to the field name as it is in the database. Instead, they should match the property name as defined in the model.
So for example:
[Column("Organisation_Account_Manager")]
[Display(Name = "Organisation_Account_Manager_ID")]
[DisplayFormat(NullDisplayText = "Unknown")]
public int? Organisation_Account_Manager_ID { get; set; }
[Display(Name = "Account Manager")]
[ForeignKey("Organisation_Account_Manager_ID")]
public Contact Account_Manager { get; set; }
In this example it will work, because [ForeignKey("Organisation_Account_Manager_ID")] is an exact match to public int? Organisation_Account_Manager_ID. Previously my [ForeignKey] annotation was using Organisation_Account_Manager, which is the field name in the database -- but this was incorrect.
I have following entity class called Code. It stores categories of different kinds - the data for which I would have otherwise needed to create many small tables e.g. User Categories, Expense Categories, Address types, User Types, file formats etc.
public class Code
{
public int Id { get; set; }
public string CodeType { get; set; }
public string CodeDescription { get; set; }
public virtual ICollection<Expense> Expenses { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
:
: // many more
}
The class Expense looks like this:
public class Expense
{
public int Id { get; set; }
public int CategoryId { get; set; }
public virtual Code Category { get; set; }
public int SourceId { get; set; }
public double Amount { get; set; }
public DateTime ExpenseDate { get; set; }
}
With the above class definitions, I have established 1:many relation between Code and Expense using the CategoryId mapping.
My problem is, I want to map the SourceId field in Expense to the Code object. Which means, Expense object would contain
public Code Source { get; set; }
If I use this, at runtime I get an error about cyclic dependencies.
Can someone please help?
You will need to disable cascading delete on at least one of the two relationships (or both). EF enables cascading delete by convention for both relationships because both are required since the foreign key properties are not nullable. But SQL Server doesn't accept multiple cascading delete paths onto the same table that are introduced by the two relationships. That's the reason for your exception.
You must override the convention with Fluent API:
public class Code
{
public int Id { get; set; }
//...
public virtual ICollection<Expense> Expenses { get; set; }
//...
}
public class Expense
{
public int Id { get; set; }
public int CategoryId { get; set; }
public virtual Code Category { get; set; }
public int SourceId { get; set; }
public virtual Code Source { get; set; }
//...
}
Mapping with Fluent API;
modelBuilder.Entity<Expense>()
.HasRequired(e => e.Category)
.WithMany(c => c.Expenses)
.HasForeignKey(e => e.CategoryId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Expense>()
.HasRequired(e => e.Source)
.WithMany()
.HasForeignKey(e => e.SourceId)
.WillCascadeOnDelete(false);