实体框架? [英] Entity Framework?

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

问题描述

我正在考虑使用Entity Framework。目前,我调用Get,Update和Delete 存储过程,如果我想在其中添加一些逻辑,则具有完全的灵活性。我的数据类有时也有自定义属性和函数。我担心使用Entity Framework的
,我可能会失去这种灵活性。


1。我仍然能够做到吗?


2。我可以在我喜欢的时候绕过实体框架吗?


3。是否允许调用程序?我有一些处理耗时的程序。


谢谢。


解决方案

你好Jay,


请注意每个帖子应该是一个问题。


问题是标题,你的帖子的主体旨在通过解释,示例等扩展它。


这是线程和答案是可搜索的,使论坛成为可搜索的资源。这个想法是"你的"。线程适合所有人。


请记住以后的任何帖子。



1您可以调用存储过程 - 这些存储过程在映射它们时会成为dbcontext.table上的方法。您将获得可以使用自己的部分类或扩展方法扩展的部分类。我使用它来添加继承,提供额外的功能。
如果我只显示一段代码,可能会更容易:


这是一个示例,其中我有一个带有Product实体的Products表:

 [MetadataTypeAttribute(typeof(Product.ProductMetadata))] 
public partial class Product:BaseEntity,IEntityWithId
{
public void MetaSetUp()
{
//在wpf中,您需要显式声明元数据文件。
//在将来的EF版本中可能会有所改进。
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Product),
typeof(ProductMetadata)),
typeof(Product));
}
内部密封类ProductMetadata
{
//其中一些数据注释依赖于dataAnnotationsExtensions(Nuget包)
[必需(ErrorMessage ="产品短名称是required")]
public string ProductShortName {get;组; }

[必需(ErrorMessage ="产品重量是必需的))
[最小值(0.01,ErrorMessage ="最小重量为0.01")]
[最大值] (70.00,ErrorMessage ="我们不卖任何重量超过70Kg的东西")]
public Nullable< decimal>体重{得;组; }

[必需(ErrorMessage ="条形码是必需的))
[RegularExpression(@"[0-9] {11}


< blockquote>",ErrorMessage ="条形码必须是11位数"")]
公共字符串BarCode {get;组; }

[必需(ErrorMessage ="每个产品的价格是必需的")]
[范围(0,200,ErrorMessage ="价格必须为0 - £200")]
public Nullable< decimal> PricePer {get;组; }
private ProductMetadata()
{}
}
}


你可以在那里添加你喜欢的任何属性,方法等基类提供了一个 一堆功能。

话虽如此,我通常会倾向于使用viewmodel包装模型对象,并且有机会在不污染模型的情况下添加属性 - 例如IsDirty。


2)如果你愿意,你可以用ado打开另一个连接,或者通过sql调用pass。


例如

 sql ="从dbo.w中选择Count(blaaId),其中BlaaId = @ Id" ;; 
sqlparameters = new List< SqlParameter>
{
new SqlParameter(" @ Id",2)
};
myCount = await db.Database.SqlQuery< int>(sql,sqlparameters.ToArray())。FirstAsync();


注意< int>是从查询返回的数据类型。


3)是的,您可以调用存储过程。 使用async await和其中一个.... async方法,以避免阻塞ui线程进行长时间运行的返回数据的调用。


或者你可以像上面那样用SqlQuery调用但是使用存储的proc名称而不是"select ..."



I am considering using Entity Framework. Currently, I call Get, Update and Delete stored procedures and have full flexibility if I want to add some logic in them. Also my data classes sometimes also have custom properties and functions. I am concerned that using Entity Framework, I might lose this flexibility.

1. Will I still be able to do the above?

2. Will I be able to bypass entity framework as and when I like?

3. Does is allow calling procedures? I have some procedures that do time consuming processing.

Thanks.

解决方案

Hi Jay,

Please note that each thread is supposed to be one question.

The question is the title, the body of your post is intended to expand on that by way of explanation, example etc.

This is so the threads and answers are searchable, making the forum a searchable resource. The idea is "your" thread is for everyone.

Please bear that in mind for any future posts.

.

1) You can call stored procedures - these become methods on your dbcontext.table when you map them. You get partial classes which you can extend using your own partial classes or extension methods. I use this to add inheritance giving extra functionality. Probably easier if I just show a piece of code:

Here's an example where I have a Products table with a Product entity:

    [MetadataTypeAttribute(typeof(Product.ProductMetadata))]
    public partial class Product : BaseEntity, IEntityWithId
    {
        public void MetaSetUp()
        {
            // In wpf you need to explicitly state the metadata file.
            // Maybe this will be improved in future versions of EF.
            TypeDescriptor.AddProviderTransparent(
                new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Product),
                typeof(ProductMetadata)),
                typeof(Product));
        }
        internal sealed class ProductMetadata
        {
            // Some of these datannotations rely on dataAnnotationsExtensions ( Nuget package )
            [Required(ErrorMessage="Product Short Name is required")]
            public string ProductShortName { get; set; }
            
            [Required(ErrorMessage = "Product Weight is required")]
            [Min(0.01, ErrorMessage = "Minimum weight is 0.01")]
            [Max(70.00, ErrorMessage = "We don't sell anything weighing more than 70Kg")]
            public Nullable<decimal> Weight { get; set; }
            
            [Required(ErrorMessage = "Bar Code is required")]
            [RegularExpression(@"[0-9]{11}


", ErrorMessage="Bar codes must be 11 digits")] public string BarCode { get; set; } [Required(ErrorMessage = "Price per product is required")] [Range(0,200, ErrorMessage="Price must be 0 - £200") ] public Nullable<decimal> PricePer { get; set; } private ProductMetadata() { } } }

You can add whatever properties, methods etc you like in there and the base class provides a  bunch of functionality.

Having said that, I would usually tend to wrap a model object with a viewmodel and there's the opportunity to add properties there without polluting the model - eg IsDirty.

2) You can open up another connection with ado if you like or call pass through sql.

eg

sql = "Select Count(blaaId) from dbo.whatever where BlaaId=@Id";
                sqlparameters = new List<SqlParameter>
                    {
                        new SqlParameter("@Id", 2)
                    };
myCount = await db.Database.SqlQuery<int>(sql, sqlparameters.ToArray()).FirstAsync();

Note the <int> is the type of the data returned from the query.

3) Yes, you can call stored procedures.  Use async await and one of the ....Async methods like above to avoid blocking the ui thread for a long running call that returns data.

Or you can call using SqlQuery like the above but with a stored proc name rather than the "select..."


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

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