实体框架中不允许多重性

实体框架中不允许多重性

本文介绍了实体框架中不允许多重性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个基本数据库,但是我得到以下异常。



Bracelet_Guest_Source ::多重性在关系'Bracelet_Guest'中的角色'Bracelet_Guest_Source'中无效。由于Dependent Role属性不是关键属性,因此Dependent Role的多重性的上限必须为'*'。



模型看起来像:



I am making a basic data base but I am getting the following exception.

Bracelet_Guest_Source: : Multiplicity is not valid in Role 'Bracelet_Guest_Source' in relationship 'Bracelet_Guest'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

The Models looks like:

public class Bracelet
    {
        [Key]
        public int BraceletID { get; set; }

        public string BraceletNumber { get; set; }

        //Guest
        [ForeignKey("Guest")]
        public int GuestID { get; set; }

        public virtual  Guest Guest { get; set; }

        //Restaurant Entries
        public  virtual  RestaurantEntry RestaurantEntry { get; set; }

    }

 public class Guest
    {
        public Guest()
        {

        }

        [Key]
        public int GuestID { get; set; }

        public string FirstName { get; set; }

        public string MiddleName { get; set; }

        public string LastName { get; set; }

        public decimal Age { get; set; }

        public string Nationality { get; set; }

        public string Pin { get; set; }

        [Column(TypeName = "DateTime2")]
        public DateTime DateOfArrival { get; set; }

        [Column(TypeName = "DateTime2")]
        public DateTime DateOfDeparture { get; set; }

        public int RoomNumber { get; set; }

        public string AllInclusiveType { get; set; }

        //[ForeignKey("Bracelet")]
        //public int BraceletID { get; set; }

        public virtual Bracelet Bracelet { get; set; }
    }

 public class RestaurantEntry
    {
        private ICollection<Bracelet> bracelets;

        public RestaurantEntry()
        {
            this.bracelets = new HashSet<Bracelet>();
        }

        [Key]
        public int RestaurantEntryID { get; set; }

        //[ForeignKey("Bracelet")]
        //public int BraceletID { get; set; }

        [Column(TypeName = "DateTime2")]
        public DateTime Time { get; set; }

        //Bracelet
        public virtual ICollection<Bracelet> Bracelets
        {
            get { return bracelets; }
            set { this.bracelets = value; }
        }
    } 





关系就像这样



手镯 - 来宾 - 1对1

手镯 - 餐厅Enry - 1对多



但是无法弄清楚什么是例外。



The relationships are like this

Bracelet - Guest - 1 to 1
Bracelet - Restaurant Enry - 1 to many

But cant figure out what is the exception.

推荐答案

 public class Guest
    {
...
        // Commenting this breaks EF
        [ForeignKey("Bracelet")]
        public int BraceletID { get; set; }

        public virtual Bracelet Bracelet { get; set; }
    }





这样做到手镯:





And do this to Bracelet:

public class Bracelet
    {
...
        //Guest
        [ForeignKey("Guest")]
        public int GuestID { get; set; }

        public virtual Guest Guest { get; set; }

        // This needs to be an ICollection for 1-M
        public virtual ICollection<RestaurantEntry> RestaurantEntry { get; set; }

    }





这样做到RestaurantEntry:





And do this to RestaurantEntry:

public class RestaurantEntry
   {
      ...

       [ForeignKey("Bracelet")]
       public int BraceletID { get; set; }

       ...

       //Bracelet - Should be singular, each entry points to 1 bracelet
       public virtual Bracelet Bracelets { get; set; }
   }


这篇关于实体框架中不允许多重性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:24