类型参数 'MongoDB.Bson.ObjectId' 违反了类型参数 'TTarget' 的约束
Type argument 'MongoDB.Bson.ObjectId' violates the constraint of type parameter 'TTarget'
我正在尝试使用 MongoDB 构建一个 MVC 网站。我是 MongoDB 的新手。当我尝试将新数据插入集合时,它会抛出以下错误
Type argument 'MongoDB.Bson.ObjectId' violates the constraint of type parameter 'TTarget'.
我的插入代码如下...
public void Add<T>(T item) where T : class, new()
{
_db.GetCollection<T>().Save(item);
}
我的 IEntity 界面如下所示
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; }
}
我的实体 class 如下所示
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; }
}
这是调用插入的代码...
IBusinessArticle businessArticle = new BusinessArticle();
businessArticle.Add(new Article { Title = "Test MongoDB", Body = "Body Body Body Body Body Body" });
为什么会报违反约束的错误。我不明白。请帮忙...
如果你插入新项目,你不应该调用 Save
,你应该调用 InsertOne
:
public void Add<T>(T item) where T : class, new()
{
_db.GetCollection<T>().InsertOne(item);
}
我正在尝试使用 MongoDB 构建一个 MVC 网站。我是 MongoDB 的新手。当我尝试将新数据插入集合时,它会抛出以下错误
Type argument 'MongoDB.Bson.ObjectId' violates the constraint of type parameter 'TTarget'.
我的插入代码如下...
public void Add<T>(T item) where T : class, new()
{
_db.GetCollection<T>().Save(item);
}
我的 IEntity 界面如下所示
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; }
}
我的实体 class 如下所示
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; }
}
这是调用插入的代码...
IBusinessArticle businessArticle = new BusinessArticle();
businessArticle.Add(new Article { Title = "Test MongoDB", Body = "Body Body Body Body Body Body" });
为什么会报违反约束的错误。我不明白。请帮忙...
如果你插入新项目,你不应该调用 Save
,你应该调用 InsertOne
:
public void Add<T>(T item) where T : class, new()
{
_db.GetCollection<T>().InsertOne(item);
}