作者:静默的温柔结成思念 | 来源:互联网 | 2023-05-18 04:52
IwouldliketoextendaclassgeneratedbyLinqToSqltoinitializewhenanewobject(row)iscr
I would like to extend a class generated by Linq To Sql to initialize when a new object (=row) is created.
我想扩展由Linq To Sql生成的类,以便在创建新对象(=行)时进行初始化。
I want to add rows to a child table when a parent row is created.
我想在创建父行时向子表添加行。
I was hoping to use the Oncreated (partial) method do something like this:
我希望使用Oncreated(部分)方法做这样的事情:
partial class Product {
partial void OnCreated() {
// Fill default rows for FK relations
this.Columns.Add(new ProductColumn {
Name = "Name", ColumnType = 1
});
this.Columns.Add(new ProductColumn {
Name = "Producer", ColumnType = 2
});
}
}
The OnCreated is called every time from the constructor. So also if the object will be loaded from the database after the call to OnCreated. And if the object is loaded from the database, do not want to execute the code.
每次从构造函数调用OnCreated。因此,如果在调用OnCreated之后将从数据库加载对象。如果从数据库加载对象,则不想执行代码。
So where can I add logic to my model to initialize an object(-graph)?
那么我在哪里可以添加逻辑到我的模型来初始化一个对象(-graph)?
4 个解决方案