我有两个模型,一个是用户(User),另一个是团队(Team),如下所示:
但是我这样做会出现以下错误:
1:
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId _id { get; set; }
[Display(Name = "Password:")]
public string Password { get; set; }
[Display(Name = "Confirm:")]
public string ConfirmPassword { get; set; }
[Display(Name = "Email:")]
public string Email { get; set; }
[Display(Name = "Username:")]
public string UserName { get; set; }
[Display(Name = "Firtname:")]
public string Firstname { get; set; }
[Display(Name = "Lastname:")]
public string Lastname { get; set; }
[Display(Name = "Country:")]
public string Country { get; set; }
[Display(Name = "City:")]
public string City { get; set; }
[Display(Name = "Birthdate:")]
public int Birthdate { get; set; }
public List<Team> Teams { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId TeamID { get; set; }
public string TeamName { get; set; }
public string UserName { get; set; }
public int LeagueID { get; set; }
public List<Player> Player { get; set; }
我已经创建了一个用户,但现在我想将团队添加到我的用户中。 这是我正在使用的代码:
var databaseClient = new MongoClient(Settings.Default.FantasySportsConnectionString);
var server = databaseClient.GetServer();
var database = server.GetDatabase("Users");
var collection = database.GetCollection<User>("users");
var user = collection.AsQueryable().First(o => o._id == Session["ID"]);
user.Teams.Add(new Team { TeamID = new ObjectId(), TeamName = "Some Team" });
但是我这样做会出现以下错误:
1:
实例参数:无法将MongoDB.Driver.MongoCollection<SportsFantasy_2._0.Models.User>转换为System.Collections.IEnumerable
2:'MongoDB.Driver.MongoCollection<SportsFantasy_2._0.Models.User>'不包含定义为'AsQueryable'的内容,并且最佳的扩展方法重载'System.Linq.Queryable.AsQueryable(System.Collections.IEnumerable)'具有一些无效的参数