У меня проблема при добавлении новой таблицы в код базы данных при первом подходе.
Ошибка:
Оператор ALTER TABLE конфликтует с ограничением FOREIGN KEY "FK_dbo.tbl_Parent_dbo. tbl_Authorizarion_Parent_Id». Конфликт произошел в базе данных «adminSection.Models.Context», таблица «dbo.tbl_Authorizarion», столбец «Auth_Id».
Мой Authorizarion
класс это:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace adminSection.Models
{
[Table("tbl_Authorizarion")]
public class Authorizarion
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
[ScaffoldColumn(false)]
public int Auth_Id { get; set; }
[Required(ErrorMessage ="you must provied email address")]
[Display(Name ="Email Address")]
[DataType(DataType.EmailAddress,ErrorMessage ="the Email Address not correct")]
public string Email { get; set; }
[Required(ErrorMessage ="you must provied your password ")]
[DataType(DataType.Password)]
public string Password { get; set; }
public string User_Type { get; set; }
public int Par_Id { get; set; }
//[ForeignKey("Par_Id")]
public virtual Parent parent { get; set; }
}
}
И мой родительский класс такой:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
namespace adminSection.Models
{
[Table("tbl_Parent")]
public partial class Parent
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
[ScaffoldColumn(false)]
public int Parent_Id { get; set; }
/// <summary>
/// End Id Att
/// </summary>
[Required(ErrorMessage = "you must provide parent First Name")]
[Display(Name = "Full Name")]
public string Parent_Full_Name{ get; set; }
/// <summary>
/// End Full Name Att
/// </summary>
[Required(ErrorMessage = "you must provide parent Email Address")]
[Display(Name = "Email Address")]
[DataType(DataType.EmailAddress)]
[EmailAddress(ErrorMessage = "Correct Input")]
[StringLength(100, ErrorMessage = "at least 10 Char", MinimumLength = 10)]
public string Parent_Email { get; set; }
/// <summary>
/// End Email Att
/// </summary>
[Required(ErrorMessage = "you must provide parent phone number")]
[Display(Name = "Phone Number")]
[DataType(DataType.PhoneNumber)]
public int Parent_Phone { get; set; }
/// <summary>
/// End Phone Number Att
/// </summary>
[Required(ErrorMessage = "you must provide parent password")]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Parent_Password { get; set; }
/// <summary>
/// End Password Att
/// </summary>
public string Parent_Location { get; set; }
/// <summary>
/// End Location Att
/// </summary>
[Required(ErrorMessage = "you must provide parent status")]
[Display(Name = "Parent status")]
public string Parent_status { get; set; }
/// <summary>
/// End parent status Att
/// </summary>
public string Par_User_Type { get; set; }
public ICollection<Child> child { get; set; }
public AdminParent adm_par { get; set; }
public ICollection<Note> note { get; set; }
// Note with Parent and note is many to one
public int Auth_Id { get; set; }
//[ForeignKey("Auth_Id")]
public virtual Authorizarion auth { get; set; }
}
}
А класс dbcontext
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Reflection.Emit;
using System.Web;
namespace adminSection.Models
{
public class Context:DbContext
{
public DbSet<Admin> admin { get; set; }
public DbSet<Child> child { get; set; }
public DbSet<Parent> parent { get; set; }
public DbSet<AdminParent> adm_par { get; set; }
public DbSet<Note> note { get; set; }
public DbSet<Authorizarion> auth { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure Student & StudentAddress entity
modelBuilder.Entity<Authorizarion>()
.HasRequired(s => s.parent)
.WithRequiredPrincipal(ad => ad.auth);
}
}
}