Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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:

C#
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; }
        }
    } 


The relationships are like this

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

But cant figure out what is the exception.
Posted

1 solution

This is because you have some issues in your models.

Firstly, I think you have a fundamental issue with your data model. If bracelets and Guests are 1-1, that infers that a guest can never change bracelets, which is bad design and will bite you later, should a guest need to replace their bracelet (coming back at a later date, etc.). If that's not possible in your business rules, merge the two objects by adding a BraaceletNumber property to the Guest object and using the Guest as your relationship.

Under the current code, though, you need to do this to Guest:
C#
 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:

C#
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; }
        
    }


And do this to RestaurantEntry:

C#
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; }
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900