我試圖使用SQLite數據庫使用ADO.NET實體框架5,我能夠連接到SQLite數據庫,並能夠從數據庫中的表中查詢記錄。但是,當我試圖將記錄插入表中的SaveChanges拋出一個異常無法使用實體框架在SQLite數據庫中插入記錄5
"An error occurred while updating the entries. See the inner exception for details."
代碼如下所示:
var connectionStr = string.Format("data source={0};Version=3;UseUTF16Encoding=True;",
fileName);
var conn = new SQLiteConnection(connectionStr);
this.personDataContext = new PersonDataContext(conn);
var persons = (from person in File.ReadAllLines(fileName)
let p = person.Split(new[] { ',', ' ' },
StringSplitOptions.RemoveEmptyEntries)
select new Person
{
ID = Convert.ToInt32(p[0]),
Name = p[1],
Address = p[2],
MobNo = Convert.ToInt32(p[3]),
PhNo = Convert.ToInt32(p[4]),
Designation = p[5]
}).ToList();
if (
(this.personDataContext !=null)
&&
(this.personDataContext.Persons != null)
)
{
foreach (var person in persons)
{
this.personDataContext.Persons.Add(person);
}
var saveFailed = false;
do
{
try
{
this.personDataContext.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
saveFailed = true;
ex.Entries.Single().Reload();
}
}
while (saveFailed);
}
,當我檢查了內部異常,然後它說,主鍵未定義。因此,在將主鍵定義到表中後,每次都會出現相同的異常。
我附上示例應用程序here。使用Open Db先打開示例數據庫,然後嘗試導入CSV文件。
我使用的是代碼優先的方法,我設計了一個Model類,並將它與SQLite數據庫中的表連接起來。
請幫助解決問題。謝謝!
你能不能更新你的問題一些更多的細節?你的創建代碼是什麼樣子的,什麼是錯誤確切的錯誤信息?很高興有一個repro解決方案,但只有極少數的用戶會下載它。請考慮後面的用戶:當他們遇到類似問題時,別人會如何發現您的問題。 – nemesv 2012-07-18 10:51:39
您可以發佈內部異常消息的**確切文本**嗎? – Slauma 2012-07-18 12:48:21
內部異常是: - {「由於違反約束而中止\ r \ nPerson.ID可能不爲NULL」}。我不明白爲什麼這段代碼對於SQLite來說很奇怪,因爲代碼可以和SQL Server一起工作。 – Pankaj 2012-07-18 13:29:29