EF Foreign Key constraint - c#

I'm still studying Entity Framework and tried to create a model including the foreign keys.
But when I tried to migrate the code, I got this error
Introducing FOREIGN KEY constraint 'FK_dbo.QuestionResults_dbo.QuestionsTables_QuetionsTableId' on table 'QuestionResults' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints
These are my model classes:
public class MainDetails
{
[Key]
public int Id { get; set; }
public string Language { get; set; }
[Required]
public string CustomerName { get; set; }
[Required]
public string ContactNumber { get; set; }
public string EmailAddress { get; set; }
[DisplayName("Service Type")]
[ForeignKey("QuestionsTable")]
public int ServiceTypeId { get; set; }
public virtual QuestionsTable QuestionsTable { get; set; }
[Required]
public string VehicleNumber { get; set; }
[Required]
public string ServiceLocation { get; set; }
public string Suggestion { get; set; }
public bool Status { get; set; } = true;
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Created Date")]
public DateTime CreatedDate { get; set; } = DateTime.Now;
public virtual QuestionResults QuestionResults { get; set; }
public virtual IList<QuestionResults> QuestionResultsMainlist { get; set; }
public virtual IList<QuestionsTable> QuestionsTables { get; set; }
}
public class QuestionsTable
{
[Key]
public int Id { get; set; }
public string ServiceType { get; set; }
public string Question { get; set; }
public virtual IList<MainDetails> MainDetailsServiceType { get; set; }
public QuestionsTable()
{
MainDetailsServiceType = new List<MainDetails>();
}
}
public class QuestionResults
{
[Key]
public int Id { get; set; }
[DisplayName("MainDetail ID")]
[ForeignKey("MainDetails")]
public int MainDetailsId { get; set; }
public virtual MainDetails MainDetails { get; set; }
[DisplayName("MainDetail ID")]
[ForeignKey("QuestionsTable")]
public int QuetionsTableId { get; set; }
public virtual QuestionsTable QuestionsTable { get; set; }
[Required]
public string CustoAnswer { get; set; }
}
This is the table structure I wanted to create:

To resolve this you can use the EF Model Builder
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<QuestionsTable>()
.HasRequired(a => a.MainDetails)
.WithOptionalDependent()
.WillCascadeOnDelete(false); //This is the important row
}
You'll have to play around with the type of relationship you would like along with whether you specify explicitly the foreign key. If you haven't seen this model builder before have a read here: https://learn.microsoft.com/en-us/ef/core/modeling/
Upon breaking your scenario down I noticed some "oddities". I reduced the noise in the domain model to from your example to this
public class MainDetails
{
[Key]
public int Id { get; set; }
[ForeignKey("QuestionsTable")]
public int ServiceTypeId { get; set; }
public virtual QuestionsTable QuestionsTable { get; set; }
public virtual QuestionResults QuestionResults { get; set; }
public virtual IList<QuestionResults> QuestionResultsMainlist { get; set; }
public virtual IList<QuestionsTable> QuestionsTables { get; set; }
}
public class QuestionsTable
{
[Key]
public int Id { get; set; }
public string ServiceType { get; set; }
public string Question { get; set; }
public virtual IList<MainDetails> MainDetailsServiceType { get; set; }
}
public class QuestionResults
{
[Key]
public int Id { get; set; }
[ForeignKey("MainDetails")]
public int MainDetailsId { get; set; }
public virtual MainDetails MainDetails { get; set; }
[ForeignKey("QuestionsTable")]
public int QuetionsTableId { get; set; }
public virtual QuestionsTable QuestionsTable { get; set; }
}
I few things I noted.
MainDetails contains both One-To-Many (QuestionTable) and Many-To-Many (IList) relationships? I'm unsure on your intention
QuestionsResults contains singular relationships to both entities which aren't replicated in the QuestionTable class? that's fine if it's intentional
ServiceType is a string in QuestionsTable but you are expecting an int as the foreign key in MainDetails?

Related

There are no primary in referenced table 'x' that match referencing column foreign key ".y"

There are no primary or candidate keys in the referenced table 'dbo.Client_Master' that match the referencing column list in the foreign key 'FK_dbo.Client_Question_Master_dbo.Client_Master_client_id'.
Could not create constraint or index. See previous errors.
My Client_Master Model
public class Client_Master
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Client_Id { get; set; }
public string Client_Name { get; set; }
public string Client_Address { get; set; }
public string Client_Email { get; set; }
public string Client_Phone { get; set; }
[DefaultValue(" ")]
public string Client_Country { get; set; }
[DefaultValue(" ")]
public string Client_State { get; set; }
[DefaultValue(" ")]
public string Client_Postcode { get; set; }
public bool Is_Active { get; set; }
public long? Created_By { get; set; }
public DateTime? Created_Date { get; set; }
[ForeignKey("Business_Master")]
public long? Business_Id { get; set; }
[ForeignKey("Categories")]
public long? Category_Id { get; set; }
public virtual Categories Categories { get; set; }
public virtual Business_Master Business_Master { get; set; }
[JsonIgnore]
public virtual ICollection<Client_Question_Master> Client_Question_Master { get; set; }
}
And My Client_Question_Master Modal
public class Client_Question_Master
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long client_question_master_id { get; set; }
[ForeignKey("Client_Master")]
public long? client_id { get; set; }
public virtual Client_Master Client_Master { get; set; }
[ForeignKey("Question_Types")]
public long? question_type_id { get; set; }
public virtual Question_Types Question_Types { get; set; }
public string question { get; set; }
public long order_no { get; set; }
public bool isContribute { get; set; } = true;
[ForeignKey("Section_Master")]
public long? section_id { get; set; }
public virtual Section_Master Section_Master { get; set; }
public double amount { get; set; }
public bool isActive { get; set; } = true;
public bool isRequired { get; set; } = true;
public bool isComment { get; set; } = true;
public string values { get; set; }
public bool isRevenue { get; set; }
public bool isStaff { get; set; }
public bool isMarketing { get; set; }
public DateTime created_date { get; set; } = DateTime.Now;
}
After add-migration during updating database it is giving me error.
Your usage of the ForeignKey attribute is the wrong way round, when using a nullable foreign key.
For example you use:
[ForeignKey("Client_Master")]
public long? client_id { get; set; }
public virtual Client_Master Client_Master { get; set; }
However it should be:
public long? client_id { get; set; }
[ForeignKey("client_id")]
public virtual Client_Master Client_Master { get; set; }
You tell EntityFramework what property is the foreign key. This prevent it from creating a field that has the same datatype as the primary key.
Check your edmx file.Edmx may not have that column as Primary key in another table which you are using as foreign key.

Introducing Foreign Key Constraint may cause cycles or multiple cascade paths when property is not required

I received this error:
Introducing FOREIGN KEY constraint
'FK_dbo.STCIProductInteractiveInfoes_dbo.User_ModifyUserID' on table
'STCIProductInteractiveInfoes' may cause cycles or multiple cascade
paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify
other FOREIGN KEY constraints.
So I looked it up online (on stackoverflow) and I see that it can be because the foreign key is required. I commented out the required property and I am still getting this error.
Here is my class:
namespace PTEManager.Domain
{
public class PTEInteractiveCourse
{
[Key]
public Guid PTEInteractiveCourseId { get; set; }
[ScaffoldColumn(false)]
[Required]
public DateTime ModifyDate { get; set; }
[ScaffoldColumn(false)]
[ForeignKey("ModifyUser")]
//[Required]
public int ModifyUserId { get; set; }
public virtual OpsUser ModifyUser { get; set; }
[Display(Name = "Package")]
[Required]
public int PackageId { get; set; }
[ScaffoldColumn(false)]
[Display(Name = "")]
[Required]
public int Status { get; set; }
[Display(Name = "STCI Course")]
[ForeignKey("STCICourseID")]
[Required]
public Guid STCIProductInteractiveInfoID { get; set; }
public virtual STCIProductInteractiveInfo STCICourseID { get; set; }
}
}
and here is the code that it is crashing on:
[HttpGet]
public ActionResult Index()
{
var model = db.PTEInteractiveCourses
.Where(p => p.Status == 1)
.ToList();
return View(model);
}
When a user deletes an Interactive Course, a user should not be deleted from the user table; just the interactive course should be deleted. Which this error is happening on a LINQ query to list the data so I do not even know what deleting it would have to do with it, we will not be adding a delete function anyway. Well there will be a delete but all it will do is hide the interactive course from the user but it will save the data to prevent data loss.
I have found this code:
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
which should theoretically work because I do not need any cascading deletes for this project but where would I put this code? It says in my datacontext class but when I do I have a build error on the void keyword. Here is my datacontext class:
namespace PTEManager.Domain.Data
{
public class DataContext : DbContext, IDataContext
{
public DataContext()
: base("HelpDesk")
{
}
//public DataContext(DbConnection existingConnection, bool contextOwnsConnection)
// : base(existingConnection, contextOwnsConnection)
//{
//}
/// <summary>
/// any entity to be persisted must be declared here.
/// </summary>
///
public DbSet<OpsUser> OpsUsers { get; set; }
public DbSet<PTEInteractiveCourse> PTEInteractiveCourses { get; set; }
public DbSet<PTETrackingClass> PTETrackingClasses { get; set; }
public DbSet<STCIProductInteractiveInfo> STCIProductInteractiveInfos { get; set; }
}
}
OpsUser Class:
namespace PTEManager.Domain
{
[Table("User")]
public class OpsUser
{
[Key]
public int u_user_id { get; set; }
public Guid DepartmentID { get; set; }
[MaxLength(50)]
[Required]
public string email_addr { get; set; }
[MaxLength(30)]
[Required]
public string first_nme { get; set; }
[MaxLength(30)]
[Required]
public string last_nme { get; set; }
[Required]
public Guid msrepl_tran_version { get; set; }
[MaxLength(1)]
[Required]
public string status { get; set; }
[MaxLength(15)]
[Required]
public string user_nme { get; set; }
[Required]
public int u_branch_id { get; set; }
}
}
PTETrackingClass:
namespace PTEManager.Domain
{
public class PTETrackingClass
{
[Key]
public Guid PTETrackingClassId { get; set; }
[ScaffoldColumn(false)]
[Required]
public DateTime ModifyDate { get; set; }
[ScaffoldColumn(false)]
[ForeignKey("ModifyUser")]
[Required]
public int ModifyUserId { get; set; }
public virtual OpsUser ModifyUser { get; set; }
[Display(Name = "Extensions Allowed")]
[Required]
public int NumberOfExtensions { get; set; }
[ScaffoldColumn(false)]
[Required]
public int Status { get; set; }
[Display(Name="Tracking Class Code")]
[Required]
public Guid TrackingClassCode { get; set; }
}
}
STCIProductInteractiveInfo:
namespace PTEManager.Domain
{
public class STCIProductInteractiveInfo
{
[Key]
public Guid STCIProductInteractiveInfoID { get; set; }
public Guid MarketingTextID { get; set; }
[Required]
public DateTime ModifyDate { get; set; }
[ForeignKey("ModifyUser")]
//[Required]
public int ModifyUserID { get; set; }
public virtual OpsUser ModifyUser { get; set; }
[MaxLength(50)]
[Required]
public string STCICourseID { get; set; }
[MaxLength(100)]
[Required]
public string STCICourseName { get; set; }
public byte STCIProductEmailHTML { get; set; }
[MaxLength(255)]
public string STCIProductEmailHTMLDateType { get; set; }
public int STCIProductEmailHTMLFileLength { get; set; }
public byte STCIProductEmailText { get; set; }
[MaxLength(255)]
public string STCIProductEmailTextDataType { get; set; }
public string STCIProductEmailTextFileLength { get; set; }
[MaxLength(50)]
[Required]
public string STCITrackingClassCode { get; set; }
[MaxLength(100)]
[Required]
public string STCITrackingClassName { get; set; }
[Required]
public int u_product_id { get; set; }
}
}
You have a circular relation on your Tables to resolve you have to option:
1) If User is optional in STCIProductInteractiveInfo or PTEInteractiveCourse you could simply change
public class STCIProductInteractiveInfo // or PTEInteractiveCourse
{
[Required]
public int ModifyUserID { get; set; }
}
to
public class STCIProductInteractiveInfo // or PTEInteractiveCourse
{
// [Required] remove required attribute too
public int? ModifyUserID { get; set; }
}
2) But if you want keep User Required simply add this on your DbContext
public class DataContext : DbContext, IDataContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<STCIProductInteractiveInfo>()
.HasRequired(e => e.ModifyUser)
.WithMany()
.WillCascadeOnDelete(false);
}
}

How modelbuilder works

i have a question about relations between entities created in Code-First:
I have Models:
public class ProjectGroup
{
[Key]
public int ProjectGroupID { get; set; }
public string ProjectGroupName { get; set; }
//FK
public virtual ICollection<File> Files { get; set; }
public virtual ICollection<List> Lists { get; set; }
}
public class File
{
[Key]
public int FileID { get; set; }
public int ProjectGroupID { get; set; }
[Required]
[Display(Name="Ścieżka pliku")]
public string FilePath { get; set; }
[Required]
[Display(Name="Data Zapisu")]
[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd hh:mm:ss}", ApplyFormatInEditMode = true)]
public DateTime FileSaveDate { get; set; }//no NULL=>error
[Display(Name="Suma MD5")]
[StringLength(32)]
public string FileMD5Hash { get; set; }
public string IPHost { get; set; }
public int FileTemplateID { get; set; }
[ForeignKey("ProjectGroupID")]
public virtual ProjectGroup ProjectGroup { get; set; }
[ForeignKey("FileTemplateID")]
public virtual FileTemplate FileTemplate { get; set; }
public virtual ICollection<List> Lists { get; set; }//klucz obcy dla listy
}
public class List
{
[Key]
public int ListID { get; set; }
public int UserID { get; set; }
public int ProjectGroupID { get; set; }
public int FileID { get; set; }
public bool Modified { get; set; }
public bool Verified { get; set; }
public bool Alive { get; set; }
[ForeignKey("UserID")]
public virtual User User { get; set; } //referencja,przekazanie nazwy FK
[ForeignKey("ProjectGroupID")]
public virtual ProjectGroup ProjectGroup { get; set; }
[ForeignKey("FileID")]
public virtual File File { get; set; }
}
And Context:
public class AWZDContext : DbContext
{
public AWZDContext()
{
}
public DbSet<User> Users { get; set; }
public DbSet<File> Files { get; set; }
public DbSet<List> Lists { get; set; }
public DbSet<RemotePC> RemotePCs { get; set; }
public DbSet<UserType> UserTypes { get; set; }
public DbSet<ProjectGroup> ProjectGroups { get; set; }
public DbSet<FileTemplate> FileTemplates { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<File>()
.HasRequired(f=>f.ProjectGroup)
.WithMany(t=>t.Files)
.WillCascadeOnDelete(false);
modelBuilder.Entity<List>()
.HasRequired(c => c.ProjectGroup)
.WithMany(d=>d.Lists)
.WillCascadeOnDelete(false);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
when I created models, I got the following errors: problem with multi-cascade delete. I added modelbuilder in on model creating.
First I added no parameter in WithMany() and it doubled relations in database like this
This created many double relations between List and ProjectGroup (File, the same, read below).
when changed to WithMany(d=>d.Lists) relations looks ok, made only once like between File and ProjectGroup.
Does modelBuilder double the effect of [foreignKey] in model?
Can anyone explain how this works? Why did it double relation earlier, with no parameter in WithMany()

MVC3 EF issues on multiple table

i am developing a database with 3 tables
Trip Model:
[Key]
public int TripId { get; set; }
[ForeignKey("Driver")]
public int DriverId { get; set; }
public virtual Driver Driver { get; set; }
public string StartingPoint { get; set; }
public string Destination { get; set; }
public DateTime TimeDepart { get; set; }
public int SeatAvailable { get; set; }
public virtual ICollection<Driver> Drivers { get; set; }
public virtual ICollection<Passenger> Passengers { get; set; }
Driver model:
[Key]
public int DriverId { get; set; }
public string DriverName { get; set; }
[ForeignKey("Trip"), Column(Order = 0)]
public int TripId { get; set; }
public virtual Trip Trip { get; set; }
And last passenger model:
[Key]
public int PassengerId { get; set; }
public string PassengerName { get; set; }
[ForeignKey("Trip"), Column(Order = 1)]
public int TripId { get; set; }
public virtual Trip Trip { get; set; }
With:
public class LiveGreenContext: DbContext
{
public DbSet<Trip> Trips { get; set; }
public DbSet<Driver> Drivers { get; set; }
public DbSet<Passenger> Passengers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
And i get the following error:
Model compatibility cannot be checked because the EdmMetadata type was
not included in the model. Ensure that IncludeMetadataConvention has
been added to the DbModelBuilder conventions.
Any solutions on this issue? Thanks!
Try adding a call to the Database.SetInitializer method in the Application_Start event handler of your Global.asax:
Database.SetInitializer<ContextName>(null);
where ContextName is the name of your custom DbContext class.

ASP.NET MVC 3 EF Code First - Problems with FOREIGN KEYS

I am having trouble with SQL Server creating Foreign Key constraints when using Entity Framework Code First.
This is my scenario. I am building an application which allows us to log tickets against any of our systems and automatically assign the ticket to the relevant person.
We have Services, which can have many categories. The categories can have many subcategories. A help desk person can be assigned to Service, and Category or Subcategory.
Here are my classes:
Service.cs
public class Service
{
[Key]
public int ServiceID { get; set; }
public string Title { get; set; }
public DateTime CreatedDate { get; set; }
public HelpDeskMember CreatedBy { get; set; }
public DateTime? DeletedDate { get; set; }
public HelpDeskMember DeletedBy { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Subcategory> Subcategories { get; set; }
public virtual ICollection<HelpDeskMember> LinesOfSupport { get; set; }
}
Category.cs
public class Category
{
[Key]
public int CategoryID { get; set; }
[ForeignKey("Service")]
public int ServiceID { get; set; }
public string Title { get; set; }
public DateTime CreatedDate { get; set; }
public HelpDeskMember CreatedBy { get; set; }
public DateTime? DeletedDate { get; set; }
public HelpDeskMember DeletedBy { get; set; }
public virtual Service Service { get; set; }
public virtual ICollection<Subcategory> Subcategories { get; set; }
public virtual ICollection<HelpDeskMember> LinesOfSupport { get; set; }
}
Subcategory.cs
public class Subcategory
{
[Key]
public int SubcategoryID { get; set; }
[ForeignKey("Service")]
public int ServiceID { get; set; }
[ForeignKey("Category")]
public int CategoryID { get; set; }
public string Title { get; set; }
public DateTime CreatedDate { get; set; }
public HelpDeskMember CreatedBy { get; set; }
public DateTime? DeletedDate { get; set; }
public HelpDeskMember DeletedBy { get; set; }
public virtual Service Service { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<HelpDeskMember> LinesOfSupport { get; set; }
}
and finally HelpDeskMember.cs
public class HelpDeskMember
{
public int HelpDeskMemberID { get; set; }
public string Name { get; set; }
public bool Admin { get; set; }
public bool Available { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? LastLogin { get; set; }
public DateTime? DeletedDate { get; set; }
public DateTime? DeletedBy { get; set; }
public virtual ICollection<Service> Services { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Subcategory> Subcategories { get; set; }
}
When the Database is being initialised, I am getting the following error message:
Introducing FOREIGN KEY constraint 'Subcategory_Service' on table 'Subcategories' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.
I'm guessing the problem is with how I have defined the Key's and ForeignKey's. Any help appreciated. Thanks.
Depending on your needs. You can in configuration for your entity use WillCascadeOnDelete(false)1 or globally removing OneToManyCascadeDeleteConvention2.
Both can be set in OnModelCreating using ModelBuilder input parameter.

Categories