我无法为主键生成新的GUID ID。每当我尝试创建另一个用户时,都会收到相同的错误,因为它不会生成新的Guid ID。
违反PRIMARY KEY约束“ PK_User”。无法在对象“ dbo.User”中插入重复密钥。重复键值为(00000000-0000-0000-0000-000000000000)。该语句已终止。
码:
public partial class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public System.Guid Id { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的UserController代码
[HttpGet]
public ActionResult Create() {
return View();
}
[HttpPost]
public ActionResult Create(User user) {
if (ModelState.IsValid) {
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
Run Code Online (Sandbox Code Playgroud)
如何自动生成新的向导?
Cannot implicitly convert type
'System.Collections.Generic.List<xxxx.Models.ApplicationUser>' to
'System.Collections.Generic.IEnumerable<xxxx.User>'. An explicit
conversion exists (are you missing a cast?)
Run Code Online (Sandbox Code Playgroud)
我找不到与此问题相关的任何内容.我在Controllers文件夹中创建了一个API文件夹,在API文件夹中添加了UserController,然后编写了以下内容:
(得到错误 return _context.Users.ToList();)
namespace xxxx.Controllers.API {
public class UserController : ApiController
{
private ApplicationDbContext _context;
public UserController() {
_context = new ApplicationDbContext();
}
//GET /api/users
public IEnumerable<User> GetUsers()
{
return _context.Users.ToList(); //<-- where I get the error message
}
Run Code Online (Sandbox Code Playgroud)
这是我的用户模型:
public partial class User
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public User()
{
this.Reviews = new HashSet<Review>();
}
public System.Guid Id { get; set; } …Run Code Online (Sandbox Code Playgroud) 该.sort()功能似乎对我根本不起作用,它不会对我所做的任何事情进行排序。
我通过 Handlebars {{Book}} 显示输出
router.get("/", (req, res) => {
Book.find({ _id: req.params.id })
.sort({ 'chapters.orderIndex': 1 }) //wont sort
.then(books => {
res.render("books/index", {
books: books
})
});
});
Run Code Online (Sandbox Code Playgroud)
我也试过:
.sort({ 'Book.chapters.orderIndex': 1 })
.sort({ 'Book.date': 1 })
.sort({ 'date': 1 }) //field from Book
.sort({ date: 1 })
Run Code Online (Sandbox Code Playgroud)
也尝试过asc/desc而不是使用1/-1
知道为什么.sort()不起作用吗?
我有两张桌子Student和Course。
该表Course采用StudentId的FK关键。里面的Course表格Score是学生选修的每门课程。
现在,我如何计算Score与 相关的总平均值StudentId?
这是我得到了多远(Id=学生 id):
var avg = db.Courses.Where(c=> c.StudentId == Id ... ?
Run Code Online (Sandbox Code Playgroud)
编辑:该Course表有一些null在Score未完成cources。
如何在视图中创建值为1-10(int)的DropDownList并将所选值传递给控制器?
UserReview模型:
public System.Guid Id { get; set; }
public System.Guid UserId { get; set; }
public System.Guid ReviewId { get; set; }
public Nullable<int> Rating { get; set; }
Run Code Online (Sandbox Code Playgroud)
控制器:
public ActionResult Rating(Guid id) //got its ReviewId from previous page
{
//so far empty, no clue what to do
return Content("");
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用嵌套字段对数据进行排序,称为orderIndex.
router.get("/", (req, res) => {
Book.find({ _id: req.params.id })
.sort({ 'Book.chapters.orderIndex': "asc" }) //doesn't work
.then(books => {
res.render("books/index", {
books: books
})
});
});
Run Code Online (Sandbox Code Playgroud)
Book外观示例:
//Book
{
"_id": {
"$oid": "1234517fe46cf86900af82f"
},
"chapters": [
{
"_id": {
"$oid": "a1"
},
"title": "first book",
"orderIndex": "1",
},
{
"_id": {
"$oid": "5678798be6bb05e4427ee65"
},
"title": "second book",
"orderIndex": "2",
},
//..some more
]
}
Run Code Online (Sandbox Code Playgroud)