C# 无法使用windows服务中的实体框架向SQL数据库添加数据
我为一个需要在数据库中存储一些数据的项目编写了一个windows服务。我创建了包含所需列的表,并从数据库中创建了一个实体模型。上下文和映射由VS 2012自动完成。为了测试数据是否被保存,我硬编码了一些值并运行了服务,但数据没有保存在数据库中 下面是我编写的一个示例服务来测试这一点:C# 无法使用windows服务中的实体框架向SQL数据库添加数据,c#,sql,entity-framework,windows-services,C#,Sql,Entity Framework,Windows Services,我为一个需要在数据库中存储一些数据的项目编写了一个windows服务。我创建了包含所需列的表,并从数据库中创建了一个实体模型。上下文和映射由VS 2012自动完成。为了测试数据是否被保存,我硬编码了一些值并运行了服务,但数据没有保存在数据库中 下面是我编写的一个示例服务来测试这一点: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using Syste
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace serviceWithDatabase
{
public partial class testService : ServiceBase
{
Database1Entities db = new Database1Entities();
public testService()
{
InitializeComponent();
test();
}
protected override void OnStart(string[] args)
{
}
public void test()
{
Table t = new Table();
t.ticker = "goog";
t.Day1 = 1234;
t.Day2 = 4567;
t.Day3 = 7890.56;
db.Tables.Add(t);
db.SaveChanges();
}
protected override void OnStop()
{
}
}
}
表格的模型:
namespace serviceWithDatabase
{
using System;
using System.Collections.Generic;
public partial class Table
{
public int Id { get; set; }
public string ticker { get; set; }
public Nullable<double> Day1 { get; set; }
public Nullable<double> Day2 { get; set; }
public Nullable<double> Day3 { get; set; }
}
}
命名空间服务WithDatabase
{
使用制度;
使用System.Collections.Generic;
公共部分类表
{
公共int Id{get;set;}
公共字符串标记器{get;set;}
公共可为空的Day1{get;set;}
公共可为空的Day2{get;set;}
公共可为空的Day3{get;set;}
}
}
App.config代码:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="Database1Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database1.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
我可以通过VS 2012手动进入数据库并添加数据,但它不会通过windows服务保存数据。有人有什么建议吗?尝试删除App.config并以其他方式直接传入连接字符串
App.config对我来说似乎阻止了编译。数据库存储在项目中,而VS正在更新错误的数据库。当您将数据库放在项目文件夹之外(例如桌面上)时,它工作得非常好。这个问题花费了我一个周末的时间。希望它能帮助其他人:)您的连接字符串显示您正在使用集成安全性,因此您是否检查了运行服务的帐户是否具有访问数据库的权限?是的,这是这台计算机上唯一的帐户。该连接字符串是由VS 2012添加的,我已在不同的项目中使用此计算机上的实体框架更新了db。Windows应用程序事件日志中是否有错误?如您所知,db.savechanges()返回一个int,表示更新的行数。我将int输出到一个文件中,数字是1。因此,它正在按照实体框架更新表,但数据库没有反映这种更改。我尝试过用try-and-catch(当出现异常时,将错误输出到文件中)来包围它,但在保存时没有任何错误。