我遇到了问题,我的DbContext没有为某些特定情况更新我的DbSet中的项目。
我有两个项目,一个Backend(其中是.mdf文件和连接字符串文件)和WCF服务项目(其中是一个链接(添加现有项目)到.mdf和连接字符串文件)。
在我的后端项目中,我有以下内容:
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; }
}
}
在我的 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);
}
}
}
工作方案:
不工作的情景:
DbSet最初有4个项目
我在后端项目中的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>
我在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>
我的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>
解决方案图片可能会有所帮助:
答案 0 :(得分:0)
您不需要指定&#34; db.Dispose();&#34;
答案 1 :(得分:0)
我在想,也许我的WCF服务正在缓存GET请求?并始终发送相同的回复?我怎么能确定这个?
在这里查看我的WCF Rest Interface实现:
namespace API
{
[ServiceContract]
public interface IDiamond
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "category")]
List<Category> SelectCategory();
}
}