Click here to Skip to main content
15,881,644 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
C#
public class User
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public string Telephone { get; set; }
        public string Mail { get; set; }
        public string TcNo { get; set; }

        public virtual Room RoomID { get; set; }
    }

public class UserMap : EntityTypeConfiguration<user>
    {
        public UserMap()
        {
            HasKey(x => x.ID);
            Property(x => x.ID)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            HasRequired(x => x.RoomID)
                .WithOptional(l => l.UserRoom)
                .Map(x => x.MapKey("RoomID"));


        }
    }


-----------------Room--------------------------------
public class Room
    {
        public int ID { get; set; }
        public int? RoomNo { get; set; }
        public int NightFee { get; set; }
        public RoomType RoomType { get; set; }
        public RoomStatus RoomStatus { get; set; }
        public string Description { get; set; }
        public bool? Cleaning { get; set; }
        public bool? Available { get; set; }
        public DateTime CheckInDate { get; set; }
        public DateTime CheckOutDate { get; set; }


        public virtual User UserRoom { get; set; }
    }


public class RoomMap : EntityTypeConfiguration<room>
    {
        public RoomMap()
        {
            HasKey(x => x.ID);
            Property(x => x.ID)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        }
    }


What I have tried:

C#
addroom.UserRoom.RoomID = adduser.RoomID;
adduser.RoomID.UserRoom = addroom.UserRoom;
adduser.RoomID = addroom.UserRoom.RoomID;
Posted
Updated 25-Apr-18 1:38am
v10

The convention is: class name + "Id".

Then EF can make assumptions about an "identity" primary key.

Use:

[Key]
public int UserId {get;set;}
 
Share this answer
 
Comments
İsa KAYA 24-Apr-18 15:31pm    
Thanks But Unfortunately it is Not


Class User ID(KEY)
Class User RoomID(ForeignKey)=Class Room ID(KEY)

Example
I want to do;
table Room-------table User
--------------------ID=03
ID=05 ========RoomID=05(Table Room ID)
You've got this completely wrong. You don't need the Configuration class at all for such a simple relationship.

I'm assuming your Room class looks something like this:
C#
public class Room
{
    [Key]
    public int RoomId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

Your User class should look like this:
C#
public class User
{
    [Key]
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Gender { get; set; }
    public string Telephone { get; set; }
    public string Mail { get; set; }
    public string TcNo { get; set; }

    public virtual Room Room { get; set; }
}

Again, no configuration class is required. The Room will automatically be optional.
You also don't really need the Key attributes, because the Id name matches the class name appended with "Id". I just put them in there because I like to be more "documenting" in my code and less "convention".
 
Share this answer
 
Comments
İsa KAYA 25-Apr-18 17:47pm    
No FirstName in Room Class
Dave Kreskowiak 25-Apr-18 19:12pm    
And that means what to me? How does a Room have a first name?
İsa KAYA 26-Apr-18 8:22am    
FirstName in room class No column
Dave Kreskowiak 26-Apr-18 9:28am    
And, again, that makes no sense at all.

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