实体框架澄清 [英] Entity Framework clarification

查看:37
本文介绍了实体框架澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简要介绍在实际问题之前所做的整个结构。



所有交易都需要自定义主键。要创建密钥,使用带有名为SiteCode的输入参数的存储过程。此存储过程为应用程序中的每个事务创建序列键。对于每个事务插入,都有一个存储过程,并且此sp调用密钥在任何insert语句之前生成SP。生成的密钥是insert语句的主键。因此,实际的SiteCode已经将每个事务SP作为输入参数。并创建了DB第一个实体模型,并且还创建了所有实体类



WPF中有一个屏幕,用于捕获模型的数据客户,地址,联系人信息。

而不是逐个获取模型并保存。最好将存储过程函数(用于插入值)映射到相应的模型。

(例如)CutomerRef属性到实体框架中的sp_CreateCustomer SP



在此过程中,映射是在除SiteCode字段之外的所有字段上完成的,该字段在任何实体模型中都没有任何引用。



已经尝试为cutomerRef模型创建部分类,将此属性添加到其中并尝试使用SP进行映射。但没有成功。

另一个失败的尝试是在实体模型中创建字段并使用sp进行映射..



这是一个众所周知的事实这可以通过从存储过程调用sp来完成,但更喜欢通过Context.Customer.Add(customerModel)来完成



任何帮助将不胜感激

Brief intro about the entire structure of what was done before the actual question.

It is required to have Custom Primary keys for all transactions. To create the key, store procedure with an input parameter named "SiteCode" is used. This store procedure creates sequence keys for every transaction made in the application. For every transaction insert there is a Stored procedure and this sp's calls key generating SP before any insert statements. The generated key is the primary key to the insert statement. Thus the actual "SiteCode" has been to every transaction SP's as an input parameter. And have created DB first entity Model and also all the entity class has been created

There is a screen in WPF which capture the data for the models Customer, Address, Contact information.
Instead of getting model one by one and saving . It would be better to map the stored procedure function (used to insert the values) with respective model.
(e.g) CutomerRef attribute to sp_CreateCustomer SP in entity framework

During this process, mapping was done on all fields except except the "SiteCode" field which does not have any reference in any entity model.

Already tried creating partial class to the cutomerRef model,adding this property into it and try to map with SP. But no success.
Another failed attempt was creating the field in the entity model and mapping with the sp..

Its a known fact that this can be done by calling sp from stored procedure but would prefer to do just by Context.Customer.Add(customerModel)

Any help would be appreciated

推荐答案

我建​​议您尝试以下操作。不是很干净的方法,但我相信它会起作用。



它要求你改变 YourDatabase.Designer.cs 文件(在解决方案资源管理器中展开YourDatabase.edmx时看到的文件)。



但在此之前在C#中创建一个静态方法,调用你 sp_GeneratePrimaryKey 带有 SiteCode 参数的StoredProcedure。我们假设方法体是 Utilities.NewPrimaryKey(int SiteCode)



找到代表你的主键的属性上面提到的文件。 ie ProjectID



看起来应该是这样的



I would suggest you to try following. Not very clean approach but I am sure it will work.

It requires that you change your YourDatabase.Designer.cs file (the file you see when you expand your YourDatabase.edmx in Solution Explorer).

But before that create a static method in C# that calls you sp_GeneratePrimaryKey StoredProcedure with SiteCode parameter. let's assume that method body is Utilities.NewPrimaryKey(int SiteCode)

Find the property representing your Primary Key in the file mentioned above. i.e. ProjectID

it should look like this

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int64 ProjectID
{
    get
    {
        return _ProjectID;
    }
    set
    {
        OnProjectIDChanging(value);
        ReportPropertyChanging("ProjectID");
        _ProjectID = StructuralObject.SetValidValue(value, "ProjectID"); // change this line.
        ReportPropertyChanged("ProjectID");
        OnProjectIDChanged();
    }
}





将其更改为





change it to

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int64 ProjectID
{
    get
    {
        return _ProjectID;
    }
    set
    {
        OnProjectIDChanging(value);
        ReportPropertyChanging("ProjectID");
        _ProjectID = StructuralObject.SetValidValue(Utilities.NewPrimaryKey(value), "ProjectID"); // here just call the static method
        ReportPropertyChanged("ProjectID");
        OnProjectIDChanged();
    }
}







一旦你这样做只需将 SiteCode 作为主键值传递,它将调用存储过程并改为设置正确的值。这样你就可以调用Context.Project.Add(projectModel)



如果我想到更好的方法,我会发布另一个答案。




And once you do that just pass the SiteCode as a primary key value and it will call the Stored Procedure and set the correct value instead. This way you can call Context.Project.Add(projectModel)

I will post another answer if I think of a better way.


这篇关于实体框架澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆