简体   繁体   English

ASP.NET EF - DbContext的DbSet未被更新

[英]ASP.NET EF - DbSet of DbContext not being Updated

I am having a problem, my DbContext is not updating the items inside my DbSet for some specific cases. 我遇到了问题,我的DbContext没有为某些特定情况更新我的DbSet中的项目。

I have two projects, a Backend (where is the .mdf file and connection string file) and WCF Service Project (where is a link (add existing item) to .mdf and connection string file). 我有两个项目,一个Backend(其中是.mdf文件和连接字符串文件)和WCF服务项目(其中是一个链接(添加现有项目)到.mdf和连接字符串文件)。

In my Backend Project I have the following: 在我的后端项目中,我有以下内容:

namespace BackEnd
{
    public static class CategoryEngine
    {
        public static void InsertCategory(string categoryName, string categoryDescription)
        {
            using (var db = new DiamondsDbContext())
            {
                db.CategorySet.Add(new Category { Name = categoryName, Description = categoryDescription });
                db.SaveChanges();
                db.Dispose();
            }
        }

        public static List<Category> SelectCategory()
        {
            using (var db = new DiamondsDbContext())
            {
                return db.CategorySet.ToList();
            }
        }
}


namespace BackEnd.DAL
{

    public class DiamondsDbContext : DbContext
    {
        public DiamondsDbContext()
            : base("DefaultConnection")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public DbSet<Category> CategorySet { get; set; }
    }
}

In my WCF Service Project : 在我的WCF服务项目中

namespace API
{
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class Diamond : IDiamond
    {
        public List<Category> SelectCategory()
        {
            return CategoryEngine.SelectCategory();
        }

        public void InsertCategory(Category category)
        {
            CategoryEngine.InsertCategory(category.Name, category.Description);
        }
}
}

Working Scenarios: 工作场景:

  • I open my view in Backend ASP Web Project (Insert/Select) methods working fine every time. 我在Backend ASP Web Project(插入/选择)方法中打开我的视图,每次都正常工作。

Not Working Scenarios: 不工作的场景:

DbSet has initially 4 items DbSet最初有4件商品

My web.config file in Backend Project: 我在后端项目中的web.config文件:

<configuration>  
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings configSource="ConnectionString.config">
  </connectionStrings>
  <!-- more stuff not related with the issue -->
</configuration>

My web.config file in WCF Service Project: 我在WCF服务项目中的web.config文件:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings configSource="ConnectionString.config">
  </connectionStrings>
</configuration>

My ConnectionString.config file existing in Backend project and linked in WCF Service Project: 我的ConnectionString.config文件存在于Backend项目中并在WCF服务项目中链接:

<connectionStrings>
  <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Integrated Security=True" providerName="System.Data.SqlClient" />-->
  <!--<add name="DefaultConnection" connectionString="Server=(localdb)\diamonds4;Integrated Security=true" providerName="System.Data.SqlClient" />-->
  <add name="DefaultConnection" connectionString="Data Source=(localdb)\teste123;AttachDbFilename=|DataDirectory|77Diamonds.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Solution Image maybe it helps: 解决方案图片可能有帮助:

解决方案图片

你不需要指定“db.Dispose();”

I was thinking, maybe my WCF Service is caching the GET requests? 我在想,也许我的WCF服务正在缓存GET请求? and sending always the same response? 并始终发送相同的回复? How can I be sure of this? 我怎么能确定这个?

Check here my WCF Rest Interface implementation: 在这里查看我的WCF Rest Interface实现:

namespace API
{
    [ServiceContract]
    public interface IDiamond
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
                    ResponseFormat = WebMessageFormat.Json,
                    BodyStyle = WebMessageBodyStyle.Bare,
                    UriTemplate = "category")]
        List<Category> SelectCategory();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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