Visual Studio 洋葱建筑中两个项目的参照

35g0bw71  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(123)

我的解决方案中有两个基于Onion体系结构设计的项目。

第一个项目是Basic Information,它存储了一些通用信息,如国家、省、市......这些信息将在解决方案中的其他项目中使用。第二个项目是ProductServiceDetail,它存储了我们产品的详细信息。Basic Information和ProductServiceDetail这两个项目之间的关系是一对多。

public class ProductServiceDetail : EntityBase
{
    public string Picture { get; private set; }
    public string PictureAlt { get; private set; }
    public string PictureTitle { get; private set; }
    public bool IsRemoved { get; private set; }
    public long ProductServiceId { get; private set; }
    public ProductService ProductService { get; private set; }
    public long BaseInfoId { get; private set; }
    public BaseInfo BaseInfo { get; private set; }
}

public class BaseInfo : EntityBase
{
    public string Title { get; private set; }
    public long ParentId { get; private set; }
    public List<ProductServiceDetail> ProductServiceDetails { get; private set; }
}

由于这些是在两个不同的项目中,因此我们需要建立从BaseInfo到ProductServiceDetail以及从ProductServiceDetail到BaseInfo的双向引用。
主要问题是EF只允许我做单向引用(BaseInfo到ProductServiceDetail反之亦然),因为循环错误。有什么想法吗?

hfwmuf9z

hfwmuf9z1#

创建一个共享项目DEMO,在其中创建BaseInfoModel和ProductModel类,并添加相应的属性。
请分别将DEMO指涉至ProductServiceDetail和BaseInfo项目。
完整代码:
DEMO代码

namespace DEMO
{
public class BaseInfo1Model
{
    public string Title { get; private set; }
    public long ParentId { get; private set; }

}

public class ProductModel
{
    public string Picture { get; private set; }
    public string PictureAlt { get; private set; }
    public string PictureTitle { get; private set; }
    public bool IsRemoved { get; private set; }
    public long ProductServiceId { get; private set; }
    public long BaseInfoId { get; private set; }
    public BaseInfo1Model BaseInfo { get; private set; }

}
}

产品服务明细代码

using DEMO;
namespace ProductServiceDetail
{
public class ProductServiceDetail1
{
    public string Picture { get; private set; }
    public string PictureAlt { get; private set; }
    public string PictureTitle { get; private set; }
    public bool IsRemoved { get; private set; }
    public long ProductServiceId { get; private set; }        
    public long BaseInfoId { get; private set; }
    public BaseInfo1Model BaseInfo { get; private set; }

}
}

基本信息代码

using DEMO;
namespace BaseInfo1
{
public class BaseInfo
{
    public string Title { get; private set; }
    public long ParentId { get; private set; }
    public List<ProductModel> ProductServiceDetails { get; private set; }
    public BaseInfo(string title, long parentId)
    {
        Title = title;
        ParentId = parentId;
        ProductServiceDetails = new List<ProductModel>();
    }
}
}

希望能对你有所帮助。

相关问题