类用户roomid(FK)= efcodefirst中的class room ID(PK)一对一关系 [英] Class user roomid(FK) = class room ID(PK) one to one relationship in efcodefirst

查看:141
本文介绍了类用户roomid(FK)= efcodefirst中的class room ID(PK)一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

        }
    }





我的尝试: < br $>




What I have tried:

addroom.UserRoom.RoomID = adduser.RoomID;
adduser.RoomID.UserRoom = addroom.UserRoom;
adduser.RoomID = addroom.UserRoom.RoomID;

推荐答案

约定是:类名+Id。



然后EF可以对身份主键做出假设。



使用:



[Key]

public int UserId {get; set;}
The convention is: class name + "Id".

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

Use:

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


你完全错了。你根本不需要Configuration类来实现这么简单的关系。



我假设你的Room类看起来像这样:

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:
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:

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



同样,不需要配置类。房间将自动成为可选项。

您也不需要Key属性,因为Id名称与附加Id的类名匹配。我只是把它们放在那里,因为我喜欢在我的代码中更多地记录而不是常规。


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".


这篇关于类用户roomid(FK)= efcodefirst中的class room ID(PK)一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆