【问题标题】:I have problem when add new table in database在数据库中添加新表时出现问题
【发布时间】:2020-07-11 04:46:32
【问题描述】:

在数据库代码优先方法中添加新表时遇到问题。

错误是:

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);
        }
    }
}

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 model-view-controller


    【解决方案1】:

    您所拥有的是一对一的映射问题。而且解决起来真的很简单。使用数据注释。示例如下:

        public class Artist {
            [Key]
            public int Id { get; set; }
            public string Name { get; set; }
            public string StageName { get; set; }
            public virtual ArtistDetails ArtistDetails { get; set; }
        }
    
        public class ArtistDetails {
           [Key]
           [ForeignKey("Artist")]
           public int ArtistID { get; set; }
           public virtual Artist Artist { get; set; }
           public string Bio { get; set; }
       }
    

    这里ArtistArtistDetail之间是一对一的关系

    【讨论】:

    • 当像这样应用您的解决方案时,我遇到此错误 类型“adminSection.Models.Admin”的属性“Auth_Id”上的 ForeignKeyAttribute 无效。在依赖类型“adminSection.Models.Admin”上找不到导航属性“Authorizarion”。 Name 值应该是有效的导航属性名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    相关资源
    最近更新 更多