问题:

我正在使用自动实现的属性。 我猜想解决以下问题的最快方法是声明我自己的后备变量?

public Point Origin { get; set; }

Origin.X = 10; // fails with CS1612

错误消息:无法修改“表达式”的返回值,因为它不是变量

试图修改作为中间表达式结果的值类型。 因为该值不是持久性的,所以该值将保持不变。

要解决此错误,请将表达式的结果存储在中间值中,或对中间表达式使用引用类型。

I’m using auto-implemented properties.I guess the fastest way to fix following is to declare my own backing variable?Error Message: Cannot modify the return value of ‘expression’ because it is not a variableAn attempt was made to modify a value type that was the result of an intermediate expression.Because the value is not persisted, the value will be unchanged.To resolve this error, store the result of the expression in an intermediate value, or use a reference type for the intermediate expression.

问题:

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Controllers
{
    public class studentsController : Controller
    {
        //
        // GET: /students/

        public ActionResult details()
        {
            int id = 16;
            studentContext std = new studentContext();
           student first = std.details.Single(m => m.RollNo == id);
            return View(first);
        }

    }
}

DbContext模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcApplication1.Models
{
    public class studentContext : DbContext
    {
        public DbSet<student> details { get; set; }
    }
}

模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Models
{
    [Table("studentdetails")]
    public class student
    {
        public int RollNo;
        public string Name;
        public string Stream;
        public string Div;
    }
}

数据库表:

CREATE TABLE [dbo].[studentdetails](
    [RollNo] [int] NULL,
    [Name] [nvarchar](50) NULL,
    [Stream] [nvarchar](50) NULL,
    [Div] [nvarchar](50) NULL
)  

在global.asax.cs中

Database.SetInitializer<MvcApplication1.Models.studentContext>(null);

上面的代码列出了我正在处理的所有类。 在运行我的应用程序时收到错误:

“模型生成期间检测到一个或多个验证错误”以及“实体类型未定义键”。

Controller:DbContext Model:Model:Database table:In global.asax.csThe above code lists all the classes I am working on.Upon running my application am receiving the error:”One or more validation errors were detected during model generation” along with “Entity type has no key defined”.

问题:

private string? typeOfContract
{
  get { return (string?)ViewState["typeOfContract"]; }
  set { ViewState["typeOfContract"] = value; }
}

稍后在代码中,我将这样使用它:

typeOfContract = Request.QueryString["type"];

typeOfContract行的声明中typeOfContract以下错误:

为了在通用类型或方法“ System.Nullable <T>”中将其用作参数“ T”,类型“字符串”必须为非空值类型

有任何想法吗? 基本上,我想在执行操作之前确保QueryString存在"type"

Later in the code I use it like this:I am getting the following error at the declaration of typeOfContract line stating:The type ‘string’ must be a non-nullable value type in order to use it as parameter ‘T’ in the generic type or method ‘System.Nullable<T>’Any ideas?Basically, I want to make sure that "type" exists in the QueryString before performing an action.

问题:

我的代码是

public class Parent
{

    public Parent(int i)
    {
        Console.WriteLine("parent");
    }
}

public class Child : Parent
{
    public Child(int i)
    {
        Console.WriteLine("child");
    }

}

我收到错误:

Parent不包含带0参数的构造函数。

我理解问题是Parent没有带0参数的构造函数。 但我的问题是,为什么我们需要一个零参数的构造函数? 没有它,为什么代码不工作?

My code isI am getting the error:Parent does not contain a constructor that takes 0 arguments.I understand the problem is that Parent has no constructor with 0 arguments.But my question is, why do we need a constructor with zero arguments?Why doesn’t the code work without it?

问题:

我创建了一个.NET Core MVC应用程序,并使用依赖注入和存储库模式将存储库注入到控制器中。 但是,我得到一个错误:

InvalidOperationException:尝试激活“ WebApplication1.Controllers.BlogController”时,无法解析类型为“ WebApplication1.Data.BloggerRepository”的服务。

型号(Blog.cs)

namespace WebApplication1.Models
{
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    }
}

DbContext(BloggingContext.cs)

using Microsoft.EntityFrameworkCore;
using WebApplication1.Models;

namespace WebApplication1.Data
{
    public class BloggingContext : DbContext
    {
        public BloggingContext(DbContextOptions<BloggingContext> options)
            : base(options)
        { }
        public DbSet<Blog> Blogs { get; set; }
    }
}

储存库(IBloggerRepository.cs和BloggerRepository.cs)

using System;
using System.Collections.Generic;
using WebApplication1.Models;

namespace WebApplication1.Data
{
    internal interface IBloggerRepository : IDisposable
    {
        IEnumerable<Blog> GetBlogs();

        void InsertBlog(Blog blog);

        void Save();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using WebApplication1.Models;

namespace WebApplication1.Data
{
    public class BloggerRepository : IBloggerRepository
    {
        private readonly BloggingContext _context;

        public BloggerRepository(BloggingContext context)
        {
            _context = context;
        }

        public IEnumerable<Blog> GetBlogs()
        {
            return _context.Blogs.ToList();
        }

        public void InsertBlog(Blog blog)
        {
            _context.Blogs.Add(blog);
        }

        public void Save()
        {
            _context.SaveChanges();
        }

        private bool _disposed;

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    _context.Dispose();
                }
            }
            _disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}

Startup.cs(相关代码)

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<BloggingContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddScoped<IBloggerRepository, BloggerRepository>();

    services.AddMvc();

    // Add application services.
    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<ISmsSender, AuthMessageSender>();
}

控制器(BlogController.cs)

using System.Linq;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Data;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class BlogController : Controller
    {
        private readonly IBloggerRepository _repository;

        public BlogController(BloggerRepository repository)
        {
            _repository = repository;
        }

        public IActionResult Index()
        {
            return View(_repository.GetBlogs().ToList());
        }

        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(Blog blog)
        {
            if (ModelState.IsValid)
            {
                _repository.InsertBlog(blog);
                _repository.Save();
                return RedirectToAction("Index");
            }
            return View(blog);
        }
    }
}

我不确定自己在做什么错。 有任何想法吗?

I created an .NET Core MVC application and use Dependency Injection and Repository Pattern to inject a repository to my controller.However, I am getting an error:InvalidOperationException: Unable to resolve service for type ‘WebApplication1.Data.BloggerRepository’ while attempting to activate ‘WebApplication1.Controllers.BlogController’.Model (Blog.cs)DbContext (BloggingContext.cs)Repository (IBloggerRepository.cs & BloggerRepository.cs)Startup.cs (relevant code)Controller (BlogController.cs)I’m not sure what I’m doing wrong.Any ideas?