简体   繁体   English

sqlite-net无法识别父类的主键

[英]sqlite-net is not able to recognize primary keys from parent class

I have my class FileDownloadContentModel which extends IBaseContentModel 我有我的类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; }
 }

Here is IBaseContentModel : 这是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;
    }
 }

For some reason sqlite-net is unable to recognize the primary key when it is getting the table mappings. 由于某些原因,sqlite-net在获取表映射时无法识别主键。 It states that the primary key is null. 它指出主键为空。 If I include the id (primary key) directly in my FileDownloadContentModel class, it is able to recognize the primary key as id. 如果我直接在我的FileDownloadContentModel类中包含id(主键),则它可以将主键识别为id。

Is this is a known bug or am I doing something wrong here? 这是一个已知的错误,还是我在这里做错了什么? Any help is appreciated, thank you! 任何帮助表示赞赏,谢谢!

UPDATED 更新

I looked into it more right now and its a stupid mistake on my part. 我现在更仔细地研究了它,这是我的愚蠢错误。 I have two "instances" of sqlite and for some reason IBaseContentModel was using a different instance of sqlite than FileDownloadContentModel which is why the primary key was not being recognized (because it wasn't from the same instance). 我有两个sqlite的“实例”,由于某种原因,IBaseContentModel使用的sqlite实例不同于FileDownloadContentModel的实例,这就是为什么不能识别主键的原因(因为它不是来自同一实例)。 And by instance i mean two completely different sqlite.cs files with different package names. 举例来说,我的意思是两个完全不同的sqlite.cs文件,它们具有不同的包名称。

It seems to be the normal behavior of .NET. 这似乎是.NET的正常行为。 A interface is not really inherited, it just forces you to implement its contract. 接口不是真正继承的,只是强制您实施其合同。 The implementation is standalone and inherits nothing from the interface. 该实现是独立的,不会从接口继承任何内容。
Thats why the following test fails: 这就是为什么以下测试失败的原因:

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);
        }
    }
}

Result: 结果:

Checking interface attribute  
Checking implementation attribute  
Assert.AreEqual failed. Expected:<1>. Actual:<0>.

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

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