简体   繁体   中英

Microsoft Entity Framework is not saving changes

Im currently trying to implement CRUD functionality with a dbfactory and generics with microsoft EF, but while listing entries is working, making changes to the db is currently not working.

 public class AbstractDataModel
    {
        [Key]
        public Guid gid { get; set; }
    }

Model

class SalesOrder : AbstractDataModel
{
    public int salesOrderID { get; set; }
    public int productID { get; set; }
    public int customerID { get; set; }
    public Guid createdBy { get; set; }
    public string dateCreated { get; set; }
    public string orderDate { get; set; }
    public string orderStatus { get; set; }
    public string dateModified { get; set; }
}

A DBCore with some other functionality besides the ones listed here, which are not relevant for the factory

 public class DBCore : DbContext
    {
    public static string connectionString = "myConnectionStringToDb";

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(connectionString);
    }

Data Service which calls factory

class SalesOrderService : DBCore
{
    public DbSet<SalesOrder> SalesOrders { get; set; }

    public OkObjectResult GetAllSalesOrders()
    {
        DBFactory factory = new DBFactory();
        return new OkObjectResult(JsonConvert.SerializeObject(factory.GetAll(SalesOrders)));
    }

    public OkObjectResult AddSalesOrder(SalesOrder order)
    {
        order.gid = Guid.NewGuid();
        return DBFactory.AddOne(order);
    }
    public OkObjectResult UpdateSalesOrder(SalesOrder order)
    {
        return DBFactory.UpdateOne(order);
    }
    public OkObjectResult DeleteSalesOrder(SalesOrder order)
    {
        return DBFactory.DeleteOne(order);
    }    
}

simple CRUD-Factory,

       class DBFactory : DBCore
{
    public DbSet<UserModel> Users { get; set; }
    public DbSet<SalesOrder> SalesOrders { get; set; }

    public List<T> GetAll<T>(DbSet<T> dbset) where T : class
    {
        using (this)
        {
            return dbset.ToList();
        }
    }
    public static OkObjectResult AddOne<T>(T data)
    {
        using (DBFactory factory = new DBFactory())
        {
            factory.Add(data);
            factory.SaveChanges();
            return new OkObjectResult("Entry was sucessfully added");
        }
    }
    public static OkObjectResult UpdateOne<T>(T data) 
    { 
        using (DBFactory factory = new DBFactory())
        {
            factory.Update(data);
            factory.SaveChanges();
            return new OkObjectResult("Entry was sucessfully updated");
        }
    }
    public static OkObjectResult DeleteOne<T>(T data)
    {
        using (DBFactory factory = new DBFactory())
        {
            factory.Attach(data);
            factory.Remove(data);
            factory.SaveChanges();
            return new OkObjectResult("Entry was sucessfully removed");
        }
    }
}

Edit: Following the advices i changed the code so it should SaveChanges for the Factory, which also contains the context as a property. But it still doesnt seem to work for all database operations except listing all entries

Editv2: Thanks for the adivces it seems i have solved that problem, but a new one appeared :DI can now do database operations like deleting entries, but now i cant list the entries anymore because the following error occurs, although the code there didnt really change:

"Executed 'GetAllOrders' (Failed, Id=5fb95793-572a-4545-ac15-76dffaa7a0cf, Duration=74ms) [2020-10-23T14:33:43.711] System.Private.CoreLib: Exception while executing function: GetAllOrders. Newtonsoft.Json: Self referencing loop detected for property 'Context' with type 'FicoTestApp.Models.SalesOrder'. Path '[0].ChangeTracker'."

try adding

services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

to your

startup.cs

it should to the job

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM