2017-02-25 12 views
1

MongoDBでMVC Webサイトを構築しようとしています。私はMongoDBの初心者です。私はコレクションに新しいデータを挿入しようとすると、それは型引数 'MongoDB.Bson.ObjectId'が型パラメータ 'TTarget'の制約に違反しています

型引数「MongoDB.Bson.ObjectId」以下のエラーがスロータイプパラメータ「TTarget」の制約に違反します。マイIEntityインターフェースは私のエンティティクラスが

public class Entity : IEntity 
{ 
    [BsonId] 
    public ObjectId Id { get; set; } 

    public DateTime CreatedDate { get; set; } 

    public DateTime LastModifiedDate { get; set; } 

    public int UserId { get; set; } 

    public bool IsActive { get; set; } 

    public bool IsDelete { get; set; } 
} 

と、この下のようなものです

public interface IEntity 
{ 
    [BsonId] 
    ObjectId Id { get; set; } 
    DateTime CreatedDate { get; set; } 
    DateTime LastModifiedDate { get; set; } 
    int UserId { get; set; } 
    bool IsActive { get; set; } 
    bool IsDelete { get; set; } 
} 

以下のようなものです

以下のように挿入するための私のコード...

public void Add<T>(T item) where T : class, new() 
{ 
    _db.GetCollection<T>().Save(item); 
} 

挿入を呼び出すコードです...

IBusinessArticle businessArticle = new BusinessArticle(); 
businessArticle.Add(new Article { Title = "Test MongoDB", Body = "Body Body Body Body Body Body" }); 

なぜ制約違反についてエラーが出るのですか?理解できません。助けてください...

答えて

2

あなたが新しい項目を挿入する場合は、あなたがSaveを呼び出すべきではありません、あなたはInsertOneを呼び出す必要があります:

public void Add<T>(T item) where T : class, new() 
{ 
    _db.GetCollection<T>().InsertOne(item); 
} 
関連する問題