在哪里放置部分类(linq2sql)
我使用 linq2sql,我有一个文件 DBDataClasses.dbml,其中包含所有映射表。 在同一个命名空间中,我有部分类,例如:
public partial Product
{
// added some logic here
}
很好,但有一个小问题。我的目录有大约 20 个文件(部分类)。 它看起来很混乱:
-Model
--Product.cs
--ProductInfo.cs
--ProductOrder.cs
and so on
我想以逻辑形式将它们分开,方式是:
-Model
**--Products** //directory / namespace
---Product.cs
---ProductInfo.cs
---ProductOrder.cs
有什么想法可以实现这一点吗?我试图使用继承:
namespace Model.Produts
class Product : Model.Product
{
// logic
}
但这不是一个好主意。
最好的问候
// 编辑:
我找到了一个非常简单的解决方案:
在 DBDataAclasses.designer.cs 中只需将模型放入另一个命名空间,例如。型号.产品
namespace Models.Products
{
[Table(Name="dbo.Products")]
public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged
{
//...
}
}
警告!将此命名空间移出根命名空间(移至文件顶部,您将看到主命名空间)
,然后为新的分部类提供相同的命名空间
Im using linq2sql and I have a file DBDataClasses.dbml which contains all mapped tables.
In the same namespace I have partial classes eg.:
public partial Product
{
// added some logic here
}
great, but there is one small problem. My directory have about 20 files (partial classes).
It looks very messy:
-Model
--Product.cs
--ProductInfo.cs
--ProductOrder.cs
and so on
I want to segregate them in a logic form, in the way:
-Model
**--Products** //directory / namespace
---Product.cs
---ProductInfo.cs
---ProductOrder.cs
Any ideas to achieve this? I was trying to use inheritance:
namespace Model.Produts
class Product : Model.Product
{
// logic
}
but its not a good idee.
Best regards
// EDIT:
I have found a solution its preatty simple:
in DBDataAclasses.designer.cs just put model to another namespace eg. Model.Products
namespace Models.Products
{
[Table(Name="dbo.Products")]
public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged
{
//...
}
}
Warning! move this name space out of the root namespace (move to the top of the file and you will see main namespace)
and then give the same namespace to the new partial class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您保持命名空间相同,您当然可以将它们放在子目录中,但我不认为这就是您想要的。
我认为要问自己的问题是,您的 DBML 中是否有太多模型/表?您可以将它们拆分为不同命名空间中的多个数据上下文,并以这种方式进行组织。如果您可以保持较小的数据上下文并专注于特定的逻辑区域(例如,一个 DBML 用于产品表,另一个 DBML 用于客户等),那就更好了。
You could certainly put them in subdirectories if you keep the namespace the same, but I don't think that's what you're going for.
I think the question to ask yourself is if you have too many models/tables in your DBML? You could split them into multiple data contexts in different namespaces and get organization that way. It's nicer if you can keep data contexts small and focused on a specific logical area, e.g., one DBML for product tables, a second DBML for customers, etc.