标签: entity-framework-5

实体框架和SQL Server 2012分页

SQL Server 2012引入了一种更有效的使用FETCH和OFFSET进行分页的机制,这可能会对使用大量分页的应用程序的性能产生很大影响.Entity Framework 5是否支持此功能?因此,如果我使用EF来翻页使用Take + Skip,如果EF针对的是SQL Server 2012,那么LINQ查询会转换为新的2012 TSQL吗?

entity-framework sql-server-2012 entity-framework-5

9
推荐指数
2
解决办法
3048
查看次数

加载在另一个DbContext中进行的更改

我有一个WPF应用程序,其中包含一个网格,其中包含我使用EF加载的数据列表.某些其他窗口可以更改网格上加载的相同数据,但使用不同的dbcontext实例.如何查看网格上已更改的数据?我知道我可以刷新一个实体ctx.Entry<MyEntity>(instance).Reload();- 但我希望看到所有的变化,无论我做什么,我只看到旧的价值观.在这种情况下,我既不能使用AsNoTracking也不能创建新DbContext实例.

entity-framework ef-code-first entity-framework-5

9
推荐指数
1
解决办法
3225
查看次数

Entity Framework 5.0 PostgreSQL(Npgsql)默认连接工厂

我试图让EF 5.0代码首先使用PostgreSQL(Npgsql提供程序).我通过NuGet安装了Npgsql 2.0.12.1(虽然引用的程序集是2.0.12.0).我在app.config中声明了Npgsql(默认连接工厂和提供者工厂):

<entityFramework>   
    <defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
 </entityFramework>
 <system.data>
        <DbProviderFactories>
          <add name="Npgsql Data Provider"
                invariant="Npgsql"
                description="Data Provider for PostgreSQL"
                support="FF"
                type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"/>
        </DbProviderFactories>
 </system.data>
Run Code Online (Sandbox Code Playgroud)

我有以下测试成功运行:

[Test]
public void DatabaseConnection_DatabaseFactoryTest()
{
    var factory = DbProviderFactories.GetFactory("Npgsql");
    var conn = factory.CreateConnection();
    conn.ConnectionString = _connectionString;
    var npg = (NpgsqlConnection)conn;
    var result = TestConnectionHelper(npg); // scalar select version(), nothing particular
    Assert.AreEqual(result, "PostgreSQL 9.2.2, compiled by Visual C++ build 1600, 64-bit");            
}
Run Code Online (Sandbox Code Playgroud)

这意味着至少数据库实例正在运行且提供程序已成功配置.现在我想要的是使用从DbContext继承的自定义数据库上下文,它将绑定到同一个提供程序并通过连接字符串初始化:

public class InventoryContext : DbContext
{ …
Run Code Online (Sandbox Code Playgroud)

.net c# database entity-framework-5 postgresql-9.2

9
推荐指数
1
解决办法
1万
查看次数

如何在实体框架代码中首先映射不同数据类型的列和实体

我正在使用Entity Framework 5 - Code.

我有一个数据库,我连接到已经存在一段时间了(我没有创建它).有一张叫做的桌子T_Customers.它包含所有客户的列表.它具有以下结构(仅部分显示):

Customer_id | numeric (5, 0) | auto increment | not null (not set as primary key)
FName | varchar(50) | not null
LName | varchar(50) | not null
Run Code Online (Sandbox Code Playgroud)

我的Customer班级:

public class Customer : IEntity
{
     public int Id { get; set; }

     public string FirstName { get; set; }

     public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的IEntity界面:

public interface IEntity
{
     int Id { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-5

9
推荐指数
1
解决办法
1万
查看次数

DbGeography.PointFromText 24141:预计在某个位置有一个数字

我有WCF REST服务,其中包含以下代码

var geo = DbGeography.PointFromText(string.Format("POINT({0} {1})", longitude, latitude), DbGeography.DefaultCoordinateSystemId);
Run Code Online (Sandbox Code Playgroud)

单元测试工作正常,但是当我从客户端或HTTP debuder调用此代码时,提供纬度和经度的任何值exept 0,它会失败,但异常:

"24141: A number is expected at position X of the input. The input has ,XXXXXX."
   at Microsoft.SqlServer.Types.WellKnownTextReader.RecognizeDouble()
   at Microsoft.SqlServer.Types.WellKnownTextReader.ParsePointText(Boolean parseParentheses)
   at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType type)
   at Microsoft.SqlServer.Types.WellKnownTextReader.Read(OpenGisType type, Int32 srid)
   at Microsoft.SqlServer.Types.SqlGeography.ParseText(OpenGisType type, SqlChars taggedText, Int32 srid)
   at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)
   at Microsoft.SqlServer.Types.SqlGeography.STPointFromText(SqlChars pointTaggedText, Int32 srid)
Run Code Online (Sandbox Code Playgroud)

latitude和longitute例如
lat:37.58336895 long:-122.40549454
lat:37.38931302 long:-122.16207476

我使用从SQLServer安装目录和SqlGeography.Point引用的Microsoft.SqlServer.Types来处理代码端的spartial数据.现在我想直接使用EF 5功能而不使用Microsoft.SqlServer.Types.无论有没有这个参考,都会失败.

知道什么是错的吗?

安装了.NET 4.5,SQL Server版本是Microsoft SQL Server 2008(SP3) - 10.0.5500.0(Intel X86)2011年9月22日00:28:06版权所有(c)1988-2008 Microsoft Corporation …

sql-server spatial .net-4.5 entity-framework-5

9
推荐指数
1
解决办法
4352
查看次数

将字段映射到EntityFramework 5 Code First中的枚举

所以,我有一个课程如下:

public class Message {

        public enum MessageType {
            Text = 0,
            Audio = 1,
            Image = 2
        }

        public int Uid { get; set; }
        public MessageType Type { get; set; }
        public String Text { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

如您所见,Type字段是枚举.我将数据匹配到此类的映射定义如下:

public class MessagesMap : EntityTypeConfiguration<Message> {

    public MessagesMap() {

        // Primary Key
        this.HasKey(t => t.Uid);

        // Properties
        this.Property(t => t.Text)
            .HasMaxLength(1000);

        // Table & Column Mappings
        this.ToTable("wc_messages");
        this.Property(t => t.Uid).HasColumnName("UID");
        this.Property(t => t.Type).HasColumnName("Type");
        this.Property(t => t.Text).HasColumnName("Text");

    }
} …
Run Code Online (Sandbox Code Playgroud)

entity-framework-5

9
推荐指数
1
解决办法
7121
查看次数

如何使用Entity Framework在Sql Server Express中获取下一个序列号?

现在的SQL Server 2012(包括SQL Server Express的2012)的SEQUENCE功能,就像Oracle作为解释在这里,这里这里.

我可以这样下一个sequence,SELECT NEXT VALUE FOR SeqName

但是我如何使用Entity Framework 5从我的代码中做到这一点?

entity-framework sql-server-2012 entity-framework-5

9
推荐指数
1
解决办法
6567
查看次数

如何将Connection String中的"Provider Name"添加到Context文件中?

我正在使用Entity Framework 5 Code-first approch.这是我的Context文件:

using IMS.Domain.Inventory;
using IMS.Domain.Security;
using IMS.Domain.StoredProcedures;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IMS.Domain.DBContext
{
    public class IMSDBContext : DbContext
    {
        public DbSet<ModuleAccounting> ModuleAccountings { get; set; }
        public DbSet<ModuleInfo> ModuleInfos { get; set; }
        public DbSet<ModuleType> ModuleTypes { get; set; }
        public DbSet<UserAccounting> UserAccountings { get; set; }
        public DbSet<UserGroup> UserGroups { get; set; }
        public DbSet<UserInfo> UserInfos { get; set; }


    //
    // …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc-4 ef-migrations entity-framework-5

9
推荐指数
2
解决办法
4万
查看次数

将数据库视图映射到EF 5.0 Code First w/Migrations

我正在尝试将SQL视图映射到EF 5.0 Code First w/Migrations中的实体,以便在页面上显示一些基本信息,而无需查询多个表以获取该信息(目前需要大约20秒才能加载.不是很好.).我听说有可能这样做,但我无法弄清楚或在网上找到正确的方法.

编辑:要更深入地了解我对此问题的解决方案,请阅读主题的博客文章.

这是我的观点:

CREATE VIEW [dbo].[ClientStatistics]
AS
SELECT       ROW_NUMBER() OVER (Order By c.ID) as Row, c.LegacyID, c.ID, c.ClientName, slc.AccountManager, slc.Network,
                             (SELECT        MAX(CreatedDate) AS Expr1
                               FROM            dbo.DataPeriods
                               WHERE        (ClientID = c.ID)) AS LastDataReceived,
                             (SELECT        MAX(ApprovedDate) AS Expr1
                               FROM            dbo.DataPeriods AS DataPeriods_2
                               WHERE        (ClientID = c.ID)) AS LastApproved,
                             (SELECT        MAX(ReportProcessedDate) AS Expr1
                               FROM            dbo.DataPeriods AS DataPeriods_1
                               WHERE        (ClientID = c.ID)) AS LastReportProcesssed
FROM            dbo.Clients AS c INNER JOIN
                         dbo.SLClients AS slc ON …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-code-first ef-migrations entity-framework-5

9
推荐指数
1
解决办法
1万
查看次数

实体框架手动删除的表不能从EF迁移生成

我创建了迁移并创建了数据库和表.例如,表格是

A B C D E.现在我又改变了部分代码并运行update-database命令.一切都顺利而且很好,表格显示了列.现在不小心我手动删除了两个表D and E.现在,当我尝试运行迁移时update-database.它运行正常但不会创建我手动删除的表.我试图删除现有的迁移并重新运行update-database.它给出了除两个表之外的错误."A,B,C"bla bla名称中已存在一个对象.

知道如何在不删除数据库的情况下摆脱这种情况并使用迁移重新创建已删除的表吗?因为我不想删除数据库,因为它包含其余表中的数据.如何在这种情况下继续我在数据库中有现有表,并且我不小心从SSMS手动删除了SQL Server中的几个表.

如何使用迁移重新创建表?

哦,我的实体框架版本是6.0.2

entity-framework-4 ef-migrations entity-framework-5 entity-framework-6

9
推荐指数
2
解决办法
7182
查看次数