EF 5 code先用[NotMapped]模型不是通过使用SqlQuery设置()?先用、模型、不是、EF

2023-09-06 16:32:16 作者:老纸的女人,你动不起i

可能重复:   Get在模型类实体Framwork code在NotMapped属性值首先使用LINQ

我使用实体框架5 code首先在我的项目,我的模型看起来更以下是这样的:

I am using Entity Framework 5's Code First in my project and my model looks more or less like this:

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }

    public virtual Category Parent { get; set; }

    [NotMapped]
    public int Level { get; set; }
}

生成的表格包含除等级列中的所有列(预期)

The generated table contains all columns except the Level column (as expected)

现在,我有一个存储过程,其计算范畴节点的水平相对于它的父。

Now, I have a stored procedure which calculates the Level of the Category node in relation to it's parent.

如果我在SQL Server管理工作室执行存储过程返回为在我的模型中指定的列包括的[NotMapped]等级

If I execute the stored procedure in SQL Server Mgmt Studio it returns the columns as specified in my model including the [NotMapped] Level column.

的事情是:水平不填,如果我称之为存储过程是这样的: Context.Database.SqlQuery< TModel的>(StoredProcedure时,parameterArray); ,其中的TModel 类别

The thing is: the Level column is not filled if I call the stored procedure like this: Context.Database.SqlQuery<TModel>(storedProcedure, parameterArray); where TModel is Category.

有没有什么办法来再利用这一类别模式

Is there any way to 're-use' this Category model?

推荐答案

您说清楚的财产级别是不是你的模型的一部分​​,当然​​,为此也不会被你的存储过程充满。为什么你会期望任何别的吗?

You clearly say that the property "Level" is not part of your model, therefor it of course won't be filled by your stored procedure. Why would you expect any else?

的唯一方法是将分析你的SP手的结果,并创建模型类的实例吧。

The only way would be to parse the result of your sp by hand and create instances of your model class yourself.

 
精彩推荐
图片推荐