2013-08-05 2 views
2

나는 여기 IBaseContentModelSQLite는-NET은 부모 클래스에서 기본 키를 인식 할 수 없습니다

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

를 확장 내 수업 FileDownloadContentModelIBaseContentModel입니다이 : 어떤 이유 SQLite는 그물이없는 경우

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

테이블 매핑을 가져올 때 기본 키를 인식합니다. 기본 키가 null임을 나타냅니다. 내 FileDownloadContentModel 클래스에 직접 id (기본 키)를 포함 시키면 기본 키를 id로 인식 할 수 있습니다.

이것은 알려진 버그입니까? 아니면 여기에 뭔가 잘못 있습니까? 어떤 도움을 주셔서 감사합니다, 감사합니다!

내가 더 지금 내 부분의 바보 같은 실수 그것으로 보았다

업데이트되었습니다. 두 sqlite "인스턴스"및 어떤 이유로 IBaseContentModel sqlite 다른 인스턴스를 사용하여 FileDownloadContentModel 이유는 기본 키 인식되지 않았습니다 (같은 인스턴스에서 아니기 때문에). 그리고 예를 들어 서로 다른 패키지 이름을 가진 완전히 다른 두 개의 sqlite.cs 파일을 의미합니다.

+0

난 당신이 위의 준 정확한 코드 문제를 재현 할 수없는입니다. 삽입/삭제하려고하면 SQLite-Net이 기본 키를 찾습니다. 그의 답변에서 제안한 @RoadBump와 같이 귀하의베이스가 실제로 인터페이스 일 가능성이 있습니까? –

+0

chou 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

기본이 인터페이스이면 true 일 수 있습니다. 그러나 위의 코드를 보면 기본 클래스입니다. 그러나 실제로는 실제 코드가 인터페이스 일 수 있으며 OP가 여기에 잘못 입력 한 것일 수 있습니다. –

+2

눈치 채지 못했다면 클래스 이름이 정말로 오도 된 것입니다 ... OP가 무엇을 말하는지 보겠습니다. – RoadBump

+0

자세한 답변을 보내 주셔서 감사합니다. 'BaseContentModel'대신 'IBaseContentModel'클래스 이름을 잘못 입력했습니다. 내베이스는 클래스이고 인터페이스는 아닙니다. 그러나 그것이 인터페이스를 위해 작동하지 않는다는 것을 아는 것이 좋다. 바라건대 당신의 대답은 그 문제에 부딪히는 다른 사람들을 도울 것입니다. – KrispyDonuts