【问题标题】:How to create one to one relationship in DevExpress XPO classes?如何在 DevExpress XPO 类中创建一对一的关系?
【发布时间】:2021-02-18 17:11:34
【问题描述】:

很快,

这是一个架构

用户表

分类表

  1. 用户有一个 categoryID
  2. 我只需要使用 User_ID、CategoryID、CategoryName 填充 GridControl
  3. 注意,CategoryName 只属于第二个表(Category Table)

我要做的是:

创建模型(用户,类别)注意主键是复合的,所以它必须是结构的 https://supportcenter.devexpress.com/ticket/details/a2615/xpo-how-to-map-persistent-objects-to-database-tables-with-compound-or-composite-multi#

// Composite key must be a struct
public struct user_key
{
    [Persistent("user_brn")]
    public int user_brn { get; set; }
    [Persistent("user_num")]
    public int user_num { get; set; }
}

[Persistent("Users")]
public class User : XPLiteObject
{
    public User(Session session) : base(session) { }

    [Key, Persistent] 
    public user_key key;    // user_brn, user_num

    [Persistent("user_name")]
    public string user_name { get; set; }

    [Persistent("CategoryID")]
    public int CategoryID { get; set; }

    [NonPersistent, Association("user_category")]
    public Category Category
    {
        get; set;
    }

    [PersistentAlias("Category.CategoryName")]
    public string CategoryName { get; set; }            // I think it will work. But it doesn't
}


[Persistent("Category")]
public class Category : XPLiteObject
{
    public Category(Session session) : base(session) { }

    [Key, Persistent("CategoryID")]
    public int CategoryID { get; set; }

    [Persistent("CategoryName")]
    public string CategoryName { get; set; }

    private User _user;
    [NonPersistent, Association("user_category")]
    public User User { get; set; }
}

GridView 代码

 IDataLayer dal = 
 XpoDefault.GetDataLayer(MSSqlConnectionProvider.GetConnectionString(" 
 (local)", "testdb"), AutoCreateOption.DatabaseAndSchema);
 Session session = new Session(dal);
 XPCollection collection = new XPCollection(session, typeof(User));
 gridControl1.DataSource = collection;

结果:=失败

我该怎么办?任何帮助

【问题讨论】:

    标签: c# .net devexpress gridcontrol xpo


    【解决方案1】:

    在您的 User 类中,删除属性 CategoryId 并更改您的 Category 属性,如下所示:

    [Persistent("CategoryID")]
    public Category Category
    {
        get; set;
    }
    

    并更改您的 CategoryName 属性:

    [PersistentAlias("Category.CategoryName")]
    public string CategoryName 
    { 
        get
        {
            return (string)EvaluateAlias(nameof(CategoryName));
        }
    }  
    

    Category 回到User 的引用比较棘手,因为没有持久字段来支持它。您可以通过免费加入来尝试:

    [PersistentAlias("[<User>][Category = '@This.CategoryID'].Single()")]
    public User User 
    { 
        get
        {
            return (User)EvaluateAlias(nameof(User));
        }
    }  
    

    【讨论】:

    • PS:除非您需要 XPO 映射到现有的 DB 结构,否则我建议使用 XPObject 作为基类。它提供了更大的灵活性,但需要更改架构,例如 ObjectType 列。
    • @deveton 你能检查一下我的答案是否适合你,如果满意就点赞吗?谢谢
    【解决方案2】:

    在一对一的关系中,表应该具有相同的键。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多