gpt4 book ai didi

c# - Iridium ORM - 无法创建关系。 (

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:24 28 4
gpt4 key购买 nike

我正在开发 Xamarin 应用程序作为学习练习,但我终生无法正确映射关系/按预期工作。 Iridium 一直是我选择的 ORM,因为它据说比标准 SQLite 更好地支持关系和自定义类型。我创建了一个单独的控制台应用程序来尝试对此进行调试,但再次遇到了麻烦。任何人都可以帮助指出我所缺少的吗?还是 Iridium/relationships/Types 或 ORM 有什么基本的我忽略了?

NuGet: iridium.db.sqlite

class Model
{
[Column.PrimaryKey(AutoIncrement = true)]
private int _ID;
public int ID { get { return _ID; } set { _ID = value; } }

[Column.Null]
public string Name { get; set; }

private List<ChildModel> _object;
[Relation.OneToMany]
private IEnumerable<ChildModel> _Objects
{
get { return _object; }
set { _object = value.ToList(); }
}
public List<ChildModel> Objects
{
get { return _object; }
set { _object = value; }
}

public Model()
{
_object = new List<ChildModel>();
}

}


class ChildModel
{
[Column.PrimaryKey(AutoIncrement = true)]
private int _ID;
public int ID { get { return _ID; } set { _ID = value; } }

[Column.Null]
public string Name { get; set; }


public ChildModel()
{

}
}



class Program
{
static void Main(string[] args)
{
var dbContext = new StorageContext(new SqliteDataProvider("mydbTest.sqlite"));

// Create tables (if they don't exist) yet
dbContext.CreateTable<Model>();
dbContext.CreateTable<ChildModel>();

//generate test data
Model model = new Model {Name ="Test" };
model.Objects.Add(new ChildModel { Name = "first" });
model.Objects.Add(new ChildModel { Name = "second" });
model.Objects.Add(new ChildModel { Name = "third" });

//Save Test Data in DB.
dbContext.Insert(model, obj => obj.Objects);


//TODO: read test data once we know it stores properly...
}
}

最佳答案

您的模型类应该简单得多。此外,ChildModel 需要一个指向父模型的外键:

class Model
{
[Column.PrimaryKey(AutoIncrement = true)]
public int ID { get;set; }

[Column.Null]
public string Name { get; set; }

[Relation.OneToMany(ForeignKey="ParentID")
public List<ChildModel> Objects {get;set;} = new List<ChildModel>();
}

class ChildModel
{
[Column.PrimaryKey(AutoIncrement = true)]
public int ID { get;set; }

public int ParentID {get;set;}

[Column.Null]
public string Name { get; set; }
}

关于c# - Iridium ORM - 无法创建关系。 (,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49089925/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com