开发者

Where put partial classes (linq2sql)

开发者 https://www.devze.com 2023-04-11 01:58 出处:网络
Im using linq2sql and I have a file DBDataClasses.dbml which contains all mapped tables. In the same namespace I have partial classes eg.:

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.Prod开发者_StackOverflow社区ucts

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号