标签: entity-framework-4

实体框架将复合键与某些 varchar 列错误地作为标识

我有下表:

CREATE TABLE [dbo].[MyTable](
    [timestamp] [timestamp] NOT NULL,
    [Col1] [varchar](20) NOT NULL,
    [Col2] [varchar](20) NOT NULL,
    [col3] [datetime] NOT NULL,
    [col4] [varchar](10) NOT NULL,
    [Col5] [varchar](10) NOT NULL,
    [Col6] [varchar](10) NOT NULL,
    [Col7] [decimal](38, 20) NOT NULL,
    [Col8] [decimal](38, 20) NOT NULL,
    [Col9] [datetime] NOT NULL,
 CONSTRAINT [MyTable$0] PRIMARY KEY CLUSTERED 
(
    [Col1] ASC,
    [Col2] ASC,
    [Col3] ASC,
    [Col4] ASC,
    [Col5] ASC,
    [Col6] ASC,
    [Col7] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, …
Run Code Online (Sandbox Code Playgroud)

.net entity-framework-4

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

EntityFramework:IQueryable或IEnumerable会在第一时间获得所有结果吗?

我了解到IQueryable或IEnumerable数据类型不会在第一时间返回结果,只在需要时返回它们.然而,当我在手表检查员中打开那个物体时,我看到所有的物体都在那里.

我的代码是否有任何问题,或只是因为我在手表上调用它而显示?

[当我在监视对话框中查看pendings对象时,我看到了所有列表项,但它不应该在第一个位置加载.我的接近是否有什么问题,或者只是因为我在手表上看到它而显示出来.

 public IQueryable<PurchasePendingView> PurchasePendings() {
            var pendings = db.PurchasePendingViews
                             .Where(m => m.AccountStatusID != StructAccountStatus.Succeed); // when I view it in the watch dialougebox I saw all the list items but it shouldn't load at the first place. Is there anything wrong in my approaching or is it just showing because I had call it on the watch. 
            if (ApplicationEnvironment.DEBUGGING) {
                return pendings;
            } else if (IsMobileAccount()) {
                var showroom = db.ShowRooms.FirstOrDefault(m=> m.MemberID == employee.MemberID);
                if (showroom …
Run Code Online (Sandbox Code Playgroud)

c# linq-to-entities entity-framework linq-to-sql entity-framework-4

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

使用SUM()将SQL语句转换为Linq/Entity Framework

我试图弄清楚如何使用Linq将以下SQL语句转换为Entity Framework:

SELECT SUM(Column1) AS Correct
     , SUM(Column2) AS Incorrect
     , UserName
FROM   Stats 
WHERE (StatType = 0)
GROUP BY UserName
ORDER BY UserName 
Run Code Online (Sandbox Code Playgroud)

出于此问题的目的,DB中的所有列类型都是INT类型,每个用户有多行数据.我基本上在输出中需要3列,每个用户的选择总数正确和不正确.

这看起来像是一个简单的SQL语句,但无论我在LinqPad这样的东西中尝试过,我总是会遇到错误.我必须错过一些比较简单的东西.一旦我开始添加".Sum"子句,我就会遇到编译错误等.

Stats.Where(s => s.StatType == 0)
     .GroupBy(s => s.UserName)
     .Select(x => new {  Correct = x.Column1
                       , Incorrect = x.Column2
                       , User=x.UserName})
     .ToList()
Run Code Online (Sandbox Code Playgroud)

c# sql linq entity-framework-4

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

在运行时设置实体框架连接字符串

我从AdventureWorks数据库生成了实体模型; 现在我想删除app.config中的连接字符串并在运行时设置它.在Model1.Context.cs文件中,我将构造函数设置为

public AdventureWorksEntities(string str)
        : base("name=AdventureWorksEntities")
    {
        this.Database.Connection.ConnectionString = str;
    }
Run Code Online (Sandbox Code Playgroud)

并在program.cs文件中

EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();
        ecsb.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";
        ecsb.Provider = @"System.Data.SqlClient";
        ecsb.ProviderConnectionString =
            @"data source=.\sqlexpress;initial catalog=AdventureWorks;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";

        using (var ent = new AdventureWorksEntities(ecsb.ConnectionString))
        {
            Console.WriteLine(ent.Database.Connection.ConnectionString);
            var add = ent.Addresses;
            foreach (var ad in add)
            {
                Console.WriteLine(ad.City);
            }


        }
        Console.ReadKey();
Run Code Online (Sandbox Code Playgroud)

现在它说没有找到元数据关键字.如何在运行时为entityframework设置连接字符串?

c# connection-string entity-framework-4

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

LINQ to Entities无法识别方法... get_Item(Int32)

(请在标记为重复之前阅读,因为我的特定方案是唯一的)

我有以下代码:

        // Get each treasure hunt
        var treasureHunts = dbContext.TreasureHunts.Where(i => i.UserName == User.Identity.Name).ToList();

        // Populate each treasure hunt with the list of leaderboard entries
        for (int i = 0; i <= treasureHunts.Count; i++)
        {
            treasureHunts[i].Leaderboard = dbContext.Leaderboard.Where(
                leaderboard => leaderboard.TreasureHuntId == treasureHunts[i].TreasureHuntId).ToList();
        }
Run Code Online (Sandbox Code Playgroud)

在运行该程序时,我从第二个数据库查询(dbContext.Leaderboard.Where ...)得到以下错误:

LINQ to Entities无法识别方法'QrCodeTreasureHunter.Models.TreasureHuntDetails get_Item(Int32)'方法,并且此方法无法转换为存储表达式.

在第一个查询中,我得到了与特定用户相关的每个寻宝活动.

在第二部分中,我试图遍历每个寻宝行为,并使用我的排行榜表中的相关排行榜条目填充寻宝的排行榜列表属性.

从我从阅读中理解的是,这个查询在其当前形式的Entity Framework中是不可能的.

您可以推荐哪些解决方法或解决方案来解决此问题?理想的解决方案是不需要更改数据模型.

如果它是相关的,这里是TreasureHunt模型:

    public class TreasureHuntDetails
{
    public TreasureHuntDetails()
    {
        Clues = new List<Clue>();
        Leaderboard = new List<Leaderboard>();

        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        var dcs = new …
Run Code Online (Sandbox Code Playgroud)

entity-framework entity-framework-4

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

获取错误:"指定的包含路径无效.实体类型字段'不声明导航属性"

创建方法:

 public List<Field> GetScheduleDetails()
 {    
     var schedulefields = DBcontextFactory.Context.Set<Field>).Include("ScheduleField").ToList();
 }
Run Code Online (Sandbox Code Playgroud)

使用上面的方法,我试图从两个表中获取所有join(field.fieldid = schedulefield.fieldid)记录.字段表与schedulefield表相关.对不起,如果我不熟悉技术术语.

现场型号:

public partial class Field : DOIEntity
{

    public Field()
    {
        this.FilerResponses = new HashSet<FilerResponse>();
        this.ScheduleFields = new HashSet<ScheduleField>();
    }

    public int FieldId { get; set; }
    public string FieldDisplayName { get; set; }
    public int FieldTypeId { get; set; }
    public string HelpText { get; set; }
    public Nullable<bool> OtherTextAllowed { get; set; }
    public Nullable<int> ChoiceGroupId { get; set; }

    public virtual FieldType FieldType { …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc entity-framework entity-framework-4 asp.net-mvc-3

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

如何从EF DbUpdateException获取一些其他信息

我正在使用Entity Framework 5并进行更新.我得到以下异常并使用SQL事件探查器我没有看到任何SQL发出:

    catch (DbUpdateException ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.Conflict, ex);
    }
Run Code Online (Sandbox Code Playgroud)

有这个消息说:

{System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_QuestionQuestionStatus". The conflict occurred in database "TestDb", table "dbo.QuestionStatus", column 'QuestionStatusId'.
The statement has been terminated.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-4

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

尝试从MVC/Linq读取现有数据库时出错

我继承了一个数据库,我需要查询它.

但是,当我在下面运行我的代码时,我收到错误:

{"The 'solution' property on 'QA' could not be set to a 'System.Byte' value. You must set this property to a non-null value of type 'System.Int32'. "}

表定义是:

CREATE TABLE [dbo].[qa](
[id] [int] IDENTITY(1,1) NOT NULL,
[r3_3] [tinyint] NULL,
[r6_3] [tinyint] NULL,
[r6_13] [tinyint] NULL,
[datesaved] [datetime] NULL
CONSTRAINT [PK_qa] PRIMARY KEY CLUSTERED 
(
[id] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,            ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
Run Code Online (Sandbox Code Playgroud)

所以r3_3,r_3,r6_13可以包含1,0或null. …

linq asp.net-mvc entity-framework-4 asp.net-mvc-3

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

动态构造Expression <Func <T,bool >>不适用于GreaterThen

嗨,我在Entity Framework中有一个简单的查询:

using (var db = new BookstoreContext())
{
    return db.Book
        .Where(w => w.PublishedYear > 2005)
        .ToList();
}
Run Code Online (Sandbox Code Playgroud)

现在我想将此查询更改为更动态的内容.但我想改变,不仅是常量值(2005),还有我的列字段(PublishedYear).

我正在寻找几天如何动态构建一个Expression<Func<,>>.现在我找到了这个页面,我正试图这样做.到目前为止,我来到这里:

public IEnumerable<Book> GetBooksGreaterThan(string columnName, object value)
{
    using (var db = new BookstoreContext())
    {
        return  db.Book
            .Where(GreaterThan(columnName, value))
            .ToList();
    }
}

public Expression<Func<Book, bool>> GreaterThan(string columnName, object value)
{
    var param = Expression.Parameter(typeof(Book), "w");
    var property = Expression.PropertyOrField(param, columnName);
    var body = Expression.GreaterThan(property, Expression.Constant(value));

    return Expression.Lambda<Func<Book, bool>>(body, param);
}
Run Code Online (Sandbox Code Playgroud)

但在第15行(var body …

c# expression entity-framework entity-framework-4

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

我的项目中安装了哪个版本的Entity Framework?

我刚刚使用Nuget在我的项目中安装Entity Framework 4.3.1.当我检查引用时,他们说System.Data.Entity是版本4.0.0.运行时版本是v4.0.30319.

  1. 这对EF版本4.3.1是否正确?我以为会说4.3.1..?

    另外,当我检查引用的dll(in C:\Program Files..\.NETFramework\v4.0\System.Data.Entity.dll)的目录时,文件的日期是18/03/2010.

    根据WikiPedia:

    版本4.3.1于2012年2月29日发布

  2. 这不应该意味着文件的日期应该接近2012年吗?

    当我使用Nuget时,Get-Package它说安装的版本是4.3.1,但我不想使用Nuget在其他项目上安装EF 4.3.1.

  3. 实体框架4.3.1 dll在哪里,所以我可以在其他项目中添加对它的引用?

谢谢.

.net c# entity-framework entity-framework-4 entity-framework-4.3

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