简体   繁体   English

按代码映射零或一关系

[英]Mapping by code Zero-Or-One relationship

I have the following database structure, which I cannot change : 我有以下数据库结构, 我无法更改

CREATE TABLE Users (
    ID INT NOT NULL IDENTITY(1,1),
    PRIMARY KEY(ID)
)

CREATE TABLE UserAvatars (
    UserID INT NOT NULL,
    Width INT NOT NULL,
    Height INT NOT NULL,

    PRIMARY KEY(UserID),
    FOREIGN KEY(UserID) REFERENCES Users(ID),
)

Basically, a User can have Zero-or-one avatars (I removed columns to simplify the example). 基本上,用户可以拥有零或一个头像(我删除了列以简化示例)。

class User
{
    public int ID { get; protected set; }

    public UserAvatar Avatar { get; set; }
}

class UserAvatar
{
    public User User { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
}

I am using the following mapping: 我使用以下映射:

class UserMapping : ClassMapping<User>
{
    public UserMapping() 
    {
        Id(x => x.ID, m => {
            m.Generator(Generators.Identity);
        });

        OneToOne(x => x.Avatar);
    }
}

class UserAvatarMapping : ClassMapping<UserAvatar>
{
    public UserAvatar() 
    {
        Id(x => x.User, m =>
        {
            m.Generator(Generators.Foreign<User>(u => u.ID));
        });

        Property(x => x.Width);
        Property(x => x.Height);
    }
}

However, when trying run my app, I get an NHibernate.MappingException : 但是,在尝试运行我的应用程序时,我得到一个NHibernate.MappingException

NHibernate.MappingException : Could not compile the mapping document: mapping_by_code----> NHibernate.MappingException : Could not determine type for: MyApp.Models.UserAvatar, MyApp, for columns: NHibernate.Mapping.Column(User) NHibernate.MappingException:无法编译映射文档:mapping_by_code ----> NHibernate.MappingException:无法确定类型:MyApp.Models.UserAvatar,MyApp,用于列:NHibernate.Mapping.Column(User)

I can't make any sense of this cryptic error. 我无法理解这个神秘的错误。

How do I accomplish the mapping where a User could have zero or one Avatars? 如何完成User可以拥有零个或一个头像的映射?

Taking from Radim Köhler's guidence, the final classes / mapping that solved the problem: 根据RadimKöhler的指导,解决问题的最终类/映射:

class User
{
    public int ID { get; protected set; }
    public UserAvatar Avatar { get; set; }
}

class UserAvatar
{
    public int UserID { get; protected set; }
    public User User { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
}

With the following mapping: 使用以下映射:

class UserMapping : ClassMapping<User>
{
    public UserMapping() 
    {
        Id(x => x.ID, m => {
            m.Generator(Generators.Identity);
        });

        OneToOne(x => x.Avatar, m =>
        {
            m.Cascade(Cascade.All);
            m.Constrained(false);
        });         
    }
}

class UserAvatarMapping : ClassMapping<UserAvatar>
{
    public UserAvatar() 
    {
        Id(x => x.UserID, m =>
        {
            m.Generator(Generators.Foreign<UserAvatar>(u => u.User));
        });

        OneToOne(x => x.User, m =>
        {
            m.Constrained(true);
        });         

        Property(x => x.Width);
        Property(x => x.Height);
    }
}

The ID is important almost for every entity mapped by NHibernate (ORM). 对于NHibernate(ORM)映射的每个实体,ID几乎都很重要。 So, we should extend the Avatar class: 所以,我们应该扩展Avatar类:

class UserAvatar
{
    // every(stuff) should have its ID
    public int ID { get; protected set; } 
    public User User { get; set; }
    ...

The ID value will be managed by one-to-one relationship. ID值将通过一对一的关系进行管理。 So, now let's adjust the mapping. 所以,现在让我们调整映射。

public UserAvatar() 
{
    Id(x => x.ID);

    HasOne(x => x.User)
      .Constrained()
      .ForeignKey();
    ...

public UserMapping() 
{
    Id(x => x.ID);
    HasOne(s => s.Avatar).Cascade.All();

Related and really interesting reading: 相关且非常有趣的阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core 中的零或一关系 - Zero-or-one relationship in EF Core 首先通过数据注释在EF代码中实现一对一或零关系 - Implementing One to Zero-Or-One relationship in EF Code first by data annotation 首先删除代码上的级联-一对零或一对 - Delete Cascade on Code First - One-to–Zero-or-One 一个与HasForeignKey为零或一 - One to zero-or-one with HasForeignKey Entity Framework Core 零或一到零或一关系 - Entity Framework Core zero-or-one to zero-or-one relation EF6必需与可选的关系(一对零或一对)无法正常工作 - EF6 Required-to-Optional Relationship (One-to–Zero-or-One) not working correctly EF Core 中必需到可选关系(一对零或一)的最佳方法是什么? - What is the best method for Required-to-Optional Relationship (One-to–Zero-or-One) in EF Core? EF6配置必需的可选关系(一对一或一对一) - EF6 Configuring a Required-to-Optional Relationship (One-to–Zero-or-One 一对零关系:当IDENTITY_INSERT为OFF时,无法在表中插入标识列的显式值 - One to zero-or-one relationship: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is OFF 无法使实体框架零或一到零或一的关系工作 - Can't get Entity Framework zero-or-one to zero-or-one relations to work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM