2013-08-05 21 views
2

我有我的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; 
    } 
} 

出於某種原因源碼網無法在獲取表映射時識別主鍵。它聲明主鍵是空的。如果我將id(主鍵)直接包含在我的FileDownloadContentModel類中,則它可以將主鍵識別爲id。

這是一個已知的bug或我在這裏做什麼了嗎?任何幫助表示讚賞,謝謝!

修訂

我看着它更多的現在和我的一個愚蠢的錯誤。我有sqlite的兩個「實例」出於某種原因,IBaseContentModel被使用SQLite比FileDownloadContentModel的不同實例,這就是爲什麼主鍵是不被認可(因爲它不是來自同一個實例)。而實例我的意思是兩個完全不同的sqlite.cs文件與不同的軟件包名稱。

+0

我不能跟你上面給出確切的代碼來重現問題。當我嘗試插入/刪除時,SQLite-Net找到主鍵。是否有可能您的基地實際上是一個界面,正如@RoadBump在他的回答中所建議的那樣? –

+0

感謝您對此進行調查x。我現在更加關注它,這是我的一個愚蠢的錯誤。我有sqlite的兩個「實例」出於某種原因,IBaseContentModel被使用SQLite比FileDownloadContentModel的不同實例,這就是爲什麼主鍵是不被認可(因爲它不是來自同一個實例)。而實例我的意思是兩個完全不同的sqlite.cs文件與不同的軟件包名稱。 – KrispyDonuts

回答

0

這似乎是.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>. 
+1

如果底座是接口,則可能是這樣;但是,如果您查看上面的代碼,則該基類是一個類。但是,實際的代碼確實可能是一個接口,並且OP在這裏錯誤地輸入了它。 –

+2

沒有注意到它,類名真的是誤導人......讓我們看看OP說的是什麼。 – RoadBump

+0

感謝您的詳細回覆!我輸錯了類名'IBaseContentModel'而不是'BaseContentModel',我的基類是一個類,而不是一個接口。但很高興知道它不適用於接口。希望你的回答能夠幫助那些遇到這個問題的人。 – KrispyDonuts