[英]sqlite-net is not able to recognize primary keys from parent class
我有我的類FileDownloadContentModel
,它擴展了IBaseContentModel
public class FileDownloadContentModel : IBaseContentModel
{
public string url { get; set; }
public string filename { get; set; }
public int downloadPercentage { get; set; }
public DateTime dateDownloaded { get; set; }
public byte[] content { get; set; }
}
這是IBaseContentModel
:
public class IBaseContentModel
{
[PrimaryKey]
public int id { get; set; }
public IBaseContentModel()
{
id = GenerateID();
}
protected static int GenerateID()
{
DateTime value = DateTime.UtcNow;
//create Timespan by subtracting the value provided from the Unix Epoch
TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
//return the total seconds (which is a UNIX timestamp)
return (int)span.TotalSeconds;
}
}
由於某些原因,sqlite-net在獲取表映射時無法識別主鍵。 它指出主鍵為空。 如果我直接在我的FileDownloadContentModel
類中包含id(主鍵),則它可以將主鍵識別為id。
這是一個已知的錯誤,還是我在這里做錯了什么? 任何幫助表示贊賞,謝謝!
更新
我現在更仔細地研究了它,這是我的愚蠢錯誤。 我有兩個sqlite的“實例”,由於某種原因,IBaseContentModel使用的sqlite實例不同於FileDownloadContentModel的實例,這就是為什么不能識別主鍵的原因(因為它不是來自同一實例)。 舉例來說,我的意思是兩個完全不同的sqlite.cs文件,它們具有不同的包名稱。
這似乎是.NET的正常行為。 接口不是真正繼承的,只是強制您實施其合同。 該實現是獨立的,不會從接口繼承任何內容。
這就是為什么以下測試失敗的原因:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
namespace TestProject2
{
public interface IMyInterface
{
[Description("Test Attribute")]
void Member();
}
[TestClass]
public class Class1 : IMyInterface
{
public void Member()
{
}
[TestMethod]
public void TestAttributeInheritance()
{
Console.WriteLine("Checking interface attribute");
MethodInfo info = typeof(IMyInterface).GetMethod("Member");
Assert.AreEqual(1, info.GetCustomAttributes(typeof(DescriptionAttribute), true).Length);
Console.WriteLine("Checking implementation attribute");
info = typeof(Class1).GetMethod("Member");
Assert.AreEqual(1, info.GetCustomAttributes(typeof(DescriptionAttribute), true).Length);
}
}
}
結果:
Checking interface attribute
Checking implementation attribute
Assert.AreEqual failed. Expected:<1>. Actual:<0>.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.