簡體   English   中英

實體框架不更新實體

[英]Entity Framework doesn't update entities

我正在使用 C# WPF 編寫應用程序,並且正在使用本地.mdf數據庫文件,使用實體框架來添加/更新數據。

不幸的是,我的代碼不起作用 - 沒有拋出異常,但在表中更新/添加行不會對實際數據庫文件應用任何更改。

表定義:

CREATE TABLE [dbo].[ESP] 
(
    [IdEsp]                INT          NOT NULL,
    [DisplayName]          NVARCHAR(50) NOT NULL,
    [CryptoKey]            CHAR(64)     NOT NULL,
    [IP]                   VARCHAR(15)  NOT NULL,
    [MeasurementFrequency] INT          NOT NULL,
    PRIMARY KEY CLUSTERED ([IdEsp] ASC)
);

模型類:

namespace RSZMT
{
    using System;
    using System.Collections.Generic;

    public partial class ESP
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public ESP()
        {
            this.TempMeasurement = new HashSet<TempMeasurement>();
        }

        public ESP(int idEsp, string displayName, string cryptoKey, string iP, int measurementFrequency)
        {
            this.TempMeasurement = new HashSet<TempMeasurement>();
            this.IdEsp = idEsp;
            this.DisplayName = displayName;
            this.CryptoKey = cryptoKey;
            this.IP = iP;
            this.MeasurementFrequency = measurementFrequency;
        }

        public int IdEsp { get; set; }
        public string DisplayName { get; set; }
        public string CryptoKey { get; set; }
        public string IP { get; set; }
        public int MeasurementFrequency { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<TempMeasurement> TempMeasurement { get; set; }
    }
}

C#代碼:

public class DataAccess
{
    RSZMT_DatabaseEntities dataEntities;

    private DataAccess()
    {
        dataEntities = new RSZMT_DatabaseEntities();
    }

    ~DataAccess()
    {
        dataEntities.Dispose();
    }

    private static DataAccess instance = new DataAccess();

    public static DataAccess GetInstance()
    {
        return instance;
    }

    ...    

    public void ChangeEspName(int espId, string newName)
    {
        var ent = dataEntities.ESP.Where(s => s.IdEsp == espId).Single();
        ent.DisplayName = newName;
        dataEntities.ESP.AddOrUpdate(ent);
        var number = dataEntities.SaveChanges();               
    }
}

我已經對此進行了調試,並且dataEntites.SaveChanges()返回 0 而不是 1 - 受影響的行。

你有什么想法,我的代碼有什么問題? 為什么它不起作用?

問候

編輯:在日志中有這樣的東西:

2020-01-12 13:40:59.21 spid52      Setting database option OFFLINE to ON for database 'C:\USERS\FILIP\SOURCE\REPOS\RSZMT\RSZMT\RSZMT_DATABASE.MDF'.
2020-01-12 13:41:01.54 Logon       Error: 18456, Severity: 14, State: 38.
2020-01-12 13:41:01.54 Logon       Login failed for user 'DESKTOP-P04PRDM\filip'. Reason: Failed to open the explicitly specified database 'C:\USERS\FILIP\SOURCE\REPOS\RSZMT\RSZMT\RSZMT_DATABASE.MDF'. [CLIENT: <named pipe>]
2020-01-12 13:41:04.50 spid52      Starting up database 'C:\USERS\FILIP\SOURCE\REPOS\RSZMT\RSZMT\BIN\DEBUG\RSZMT_DATABASE.MDF'.
2020-01-12 13:41:04.52 spid52      Parallel redo is started for database 'C:\USERS\FILIP\SOURCE\REPOS\RSZMT\RSZMT\BIN\DEBUG\RSZMT_DATABASE.MDF' with worker pool size [4].
2020-01-12 13:41:04.52 spid52      Parallel redo is shutdown for database 'C:\USERS\FILIP\SOURCE\REPOS\RSZMT\RSZMT\BIN\DEBUG\RSZMT_DATABASE.MDF' with worker pool size [4].
2020-01-12 13:41:11.55 Logon       Error: 18456, Severity: 14, State: 38.
2020-01-12 13:41:11.55 Logon       Login failed for user 'DESKTOP-P04PRDM\filip'. Reason: Failed to open the explicitly specified database 'C:\USERS\FILIP\SOURCE\REPOS\RSZMT\RSZMT\RSZMT_DATABASE.MDF'. [CLIENT: <named pipe>]
2020-01-12 13:41:24.23 Logon       Error: 18456, Severity: 14, State: 38.
2020-01-12 13:41:24.23 Logon       Login failed for user 'DESKTOP-P04PRDM\filip'. Reason: Failed to open the explicitly specified database 'C:\USERS\FILIP\SOURCE\REPOS\RSZMT\RSZMT\RSZMT_DATABASE.MDF'. [CLIENT: <named pipe>]

我終於做到了。 Database.mdf 文件中有一個名為“復制到輸出目錄”的屬性,默認情況下它設置為“始終復制”,我將其更改為“從不復制”,現在一切正常。 非常感謝大家的回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM