简体   繁体   English

asp.net c#和mongodb模型

[英]asp.net c# and mongodb models

I wanna know if i can use mongodb like models (model classes) in my project (asp.net mvc 4 c#). 我想知道我是否可以在我的项目中使用mongodb模型(模型类)(asp.net mvc 4 c#)。

For example: 例如:

namespace Demo.Models
{

    public class Model1
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal Price { get; set; }
    }

    public class MovieDBContext : DbContext
    {
        public DbSet<Model1> models { get; set; }
    }
}

Let's say this is standard model for mssql database. 假设这是mssql数据库的标准模型。 Can i create models like this to use MongoDB collections, documents? 我可以创建这样的模型来使用MongoDB集合,文档吗? If yes, how? 如果有,怎么样? (if you can provide me with link of some examples). (如果你能提供一些例子的链接)。

Thanks. 谢谢。

Yes you can don't forget to add BsonId attribute, because every object has to have his own unique id. 是的,您不能忘记添加BsonId属性,因为每个对象都必须拥有自己唯一的ID。

public class Model1
{
    [BsonId]
    public int ID { get; set; }
    public string Title { get; set; }
    public DateTime ReleaseDate { get; set; }
    public string Genre { get; set; }
    public decimal Price { get; set; }
}

And example you can find here: 例如,你可以在这里找到:

http://www.joe-stevens.com/2011/10/02/a-mongodb-tutorial-using-c-and-asp-net-mvc/ http://www.joe-stevens.com/2011/10/02/a-mongodb-tutorial-using-c-and-asp-net-mvc/

Yes you can use mongodb as model in your project. 是的,你可以在项目中使用mongodb作为模型。 you just have to define a model class and get the required enetities that you need.But in this you have provide an BsonId attribute for any object for generating a unique id. 你只需要定义一个模型类并获得你需要的所需的enetities。但是你已经为任何对象提供了一个BsonId属性来生成一个唯一的id。

here's is a example from my code just check it out. 这是我的代码中的一个示例,请查看它。

public class QuestionAttempt
{
    public ObjectId QId { get; set; }
    public long UserId { get; set; }
    [BsonElement("SN")]
    public string SkillName { get; set; }
    [BsonElement("IC")]
    public bool IsCorrect { get; set; }
}

In my code i have given some objects a smaller name with the [BsonElement()] attribute for less memory usage. 在我的代码中,我给了一些带有[BsonElement()]属性的较小名称的对象,以减少内存使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM