扩展默认实体框架模型类以包含自定义类

发布于 2024-10-21 23:17:53 字数 1431 浏览 1 评论 0原文

我有一个实体模型,其中添加了一些从我的数据库中添加的表。我想包含一个自定义类,它将充当数据模型并返回自定义数据。这就是我想要做的:

// My custom data model
public class DataModel
{
    var dbContext = new ODataDemoEntities();

    Employees = from e in dbContext.Employee
                select new EmployeeModel
                {
                    ID = e.EmployeeID,
                    FirstName = e.FirstName,
                    LastName = e.LastName
                };

    public IQueryable<EmployeeModel> Employees { get; private set; }
}

// My custom class
[DataServiceKey("ID")]
public class EmployeeModel
{
    /// <summary>ID of the employee.</summary>
    public int ID { get; set; }

    /// <summary>First name of the employee.</summary>
    public string FirstName { get; set; }

    /// <summary>Last name of the employee.</summary>
    public string LastName { get; set; }
}

// My WCF Data Service Code
public class EmployeeDataService : DataService<DataModel>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Employees", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

但是我想在我现有的实体数据模型类中包含这个Employees类,这样我就可以扩展现有的数据模型以包含我的自定义类(EmployeesModel),而不是使用不同的数据模型创建单独的服务) 也。

I have an entity model which has some tables added from my DB. I want to include a custom class which will act as a data model and will return customized data. Here is what I am trying to do:

// My custom data model
public class DataModel
{
    var dbContext = new ODataDemoEntities();

    Employees = from e in dbContext.Employee
                select new EmployeeModel
                {
                    ID = e.EmployeeID,
                    FirstName = e.FirstName,
                    LastName = e.LastName
                };

    public IQueryable<EmployeeModel> Employees { get; private set; }
}

// My custom class
[DataServiceKey("ID")]
public class EmployeeModel
{
    /// <summary>ID of the employee.</summary>
    public int ID { get; set; }

    /// <summary>First name of the employee.</summary>
    public string FirstName { get; set; }

    /// <summary>Last name of the employee.</summary>
    public string LastName { get; set; }
}

// My WCF Data Service Code
public class EmployeeDataService : DataService<DataModel>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Employees", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

However I want to include this Employees class in my existing entity data model class, so that rather than creating separate service with different data model, I extend my existing data model to include my custom class (EmployeesModel) too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2024-10-28 23:17:53

为什么要公开现有实体的自定义视图?无论如何,客户都可以这样做,并使用投影按照他们想要的方式定制它们。客户端的查询与上面编写的用于初始化Employees 属性的查询完全相同。

目前,还没有办法做到这一点。我们正在研究确保在下一个版本中实现这一点的方法。虽然没有任何承诺,但有一件事我们已经被问过很多次了,而且它在我们的优先级列表中相当高。

使用此投票网站对此功能进行投票:http://blogs.msdn.com/b/astoriateam/archive/2010/09/10/what-do-you-want-to -see-added-changed-in-wcf-data-services.aspx

谢谢
普拉蒂克

Why would you want to expose a customized view over an existing entity? The client can do this anyways and customize them in the way they want using projections. The query on the client side would exactly look like the one who wrote above to initialize the Employees property.

Currently, there is no way to do this. We are looking into ways to make sure happen in our next release. No promises though, but there is one of the things we have been asked a number of times and its pretty high in our proirity list.

Use this voting site to vote for this feature: http://blogs.msdn.com/b/astoriateam/archive/2010/09/10/what-do-you-want-to-see-added-changed-in-wcf-data-services.aspx

Thanks
Pratik

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文