2016-01-02 68 views
3

我的項目中有兩個dummyrepositorys,一個用於提問,一個用於答案。問題是多重選擇,所以他們可以有多個答案。我問題型號:dummyrepository數據外鍵與隨機ID

public class Question : BaseClass 
{ 
    public Question() : base() 
    { 

    } 

    public int QuestionId { get; set; } 
    public string Value { get; set; } 

    public virtual List<Answer> Answers { get; set; } 
} 

而一個回答屬於問題

public class Answer : BaseClass 
{ 
    public Answer() : base() 
    { 

    } 
    public int AnswerId { get; set; } 
    public string Value { get; set; } 

    public int QuestionId { get; set; } 
    public virtual Question Question { get; set; } 
} 

他們都延長BaseClass的其中有一些自定義字段。

public abstract class BaseClass 
{ 
    protected BaseClass() 
    { 
     UniqueIdentifier = RandomIdentifier(20); 
    } 

    public string UniqueIdentifier { get; set; } 

    private static string RandomIdentifier(int length) 
    { 
    //returns an unique identifier 
    } 
} 

dummyQuestionRepository樣子:

public class DummyQuestionRepository : IQuestionRepository 
{ 

    private List<Question> _questions; 

    public DummyQuestionRepository() 
    { 
     _questions = new List<Question>(); 
     _questions.Add(new Question { Value = "Favourit food?" }); 
     _questions.Add(new Question { Value = "Who is the president?" }); 
     _questions.Add(new Question { Value = "Favourit movie?" }); 
    } 

    public List<Question> GetAll() 
    { 
     return _questions; 
    } 
    public void Create(Question q) 
    { 
     _questions.Add(q); 
    } 

    //removed the non relevant functions 
} 

dummyAnswerRepository

class DummyAnswerRepository 
{ 
    private List<Answer> _answers; 

    public DummyAnswerRepository() 
    { 
     _answers = new List<Answer>(); 
     _answers.Add(new Answer { Value = "pizza" }); 
     _answers.Add(new Answer { Value = "fries" }); 
     _answers.Add(new Answer { Value = "Bush" }); 
     _answers.Add(new Answer { Value = "Obama" }); 
     _answers.Add(new Answer { Value = "titanic" }); 
     _answers.Add(new Answer { Value = "lion king" }); 
    } 

    public List<Answer> GetAll() 
    { 
     return _answers; 
    } 

    public void Create(Answer a) 
    { 
     _answers.Add(a); 

    } 
} 

正如你可能已經注意到了基類有一個唯一標識符的變量。此變量用於在聯機數據庫中創建唯一值(由於用戶在離線工作時可能使用相同的ID,因此無法使用該id),則應將UniqueIdentifier作爲有問題的外鍵。 我應該如何從問題中獲得/設置答案,以便我可以將它們加載到我的視圖中?

+0

:只要刪除其創建的存儲庫的構造和使用代碼的新實體像下面的代碼。當您離線時,必須爲添加的任何新項目創建一個臨時ID。然後,當您連接到在線數據庫時,必須獲取永久ID,然後您必須用永久ID替換臨時ID。 – jdweng

+0

@jdweng我真的很想使用我發佈的代碼。我不需要記住它是否同步的值以及需要在服務器的回調中設置id的值。我可能會更改代碼,但我仍然想知道如何解決我發佈的問題。 –

+0

在線數據庫是否被一個或多個用戶使用?在線連接時如何獲得ID?當多個用戶使用數據庫時,您必須確保爲每個新項目生成唯一的ID。在解決同步問題之前,您首先必須解決此問題。 – jdweng

回答

1

好方法是使用Guid這將幫助您合併用戶數據庫,因爲Guid具有2^128個唯一值。使用Guid.NewGuid產生新的獨特的價值

public abstract class BaseClass 
{ 
    protected BaseClass() 
    { 
     UniqueIdentifier = Guid.NewGuid(); 
    } 

    public Guid UniqueIdentifier { get; set; } 
} 

要添加可以實現類似的方法來Entity Framework Seed method外鍵時,答案和問題將在一個地方創建並添加到您的存儲庫。當您連接到您的在線數據庫同步,必須在本地數據庫和在線數據庫之間發生

public class DataBaseInitializer 
{ 
    public void Seed(IQuestionRepository questionRepository, DummyAnswerRepository answerRepository) 
    { 
     var q1 = new Question { Value = "Favourit food?" }; 
     var q2 = new Question { Value = "Who is the president?" }); 
     var q3 = new Question { Value = "Favourit movie?" }); 

     questionRepository.Create(q1); 
     questionRepository.Create(q2); 
     questionRepository.Create(q3); 

     answerRepository.Create(new Answer { Value = "pizza", Question = q1 }); 
     answerRepository.Create(new Answer { Value = "fries", Question = q1 }); 
     answerRepository.Create(new Answer { Value = "Bush", Question = q2 }); 
     answerRepository.Create(new Answer { Value = "Obama", Question = q2 }); 
     answerRepository.Create(new Answer { Value = "titanic", Question = q3 }); 
     answerRepository.Create(new Answer { Value = "lion king", Question = q3 }); 
    } 
} 
+0

問題是我沒有創建一個獨特的ID,我創建一個基於時間戳以毫秒爲單位的隨機36值長字符串(相等的機會小於26^36),並且它必須在毫秒。問題是關於使用隨機ID時設置外鍵的問題。 –

+0

哦,現在我明白了,對不起我的回答。對於你的問題,你應該實現類似於DataBase Seed方法的邏輯 - 你的答案應該與qustions在同一個地方創建,然後你可以做一些像'_answers.Add(new Answer {Value =「Bush」,Question = question2});' –

+0

但我怎樣才能將問題添加到答案,我應該通過構造函數中的dummyquestionrepository並在那裏得到問題? –