. NET Ядро связывает несколько объектов с пользователем - PullRequest
0 голосов
/ 12 января 2020

Я пытаюсь связать отношения «один ко многим» между пользователем и классом. Когда я пытаюсь создать сообщение, я также пытаюсь добавить его в пользовательскую модель, но я не могу понять, что это правильно.

Пользователь, который должен иметь несколько проектов enteties

public class AppUser : IdentityUser
{
    public ICollection<UserProject> Projects { get; set; }
}

Модель проекта

public class UserProject
{
    public int Id { get; set; }

    public string Name { get; set; }
}

Действие по добавлению проекта и связыванию его с пользователем

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(UserProject userProject)
    {

        if (ModelState.IsValid)
        {
            AppUser appUser = await userManager.GetUserAsync(HttpContext.User);
            appUser.Projects.Add(userProject);

            context.Projects.Add(userProject);
            await context.SaveChangesAsync();

            return RedirectToAction("Index");
        }
        return View(userProject);
    }

Однако это context.Projects.Add(userProject); приводит к ошибке NullReferenceException: Object reference not set to an instance of an object. Кто-нибудь, пожалуйста, скажите мне, что не так и как добиться того, что я пытаюсь сделать?

Контекст БД

public class ScrumApplicationContext : IdentityDbContext<AppUser>
{
    public ScrumApplicationContext(DbContextOptions<ScrumApplicationContext> options)
        : base(options)
    {
    }
    public DbSet<UserProject> Projects { get; set; }

}

Startup configureservices

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();

        services.AddDbContext<ScrumApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ScrumApplicationContext")));

        services.AddIdentity<AppUser, IdentityRole>()
            .AddEntityFrameworkStores<ScrumApplicationContext>()
            .AddDefaultTokenProviders();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "areas",
                pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
            );

            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}"
            );
        });
        CreateAdminRole(serviceProvider);
        CreateAdminUserAsync(serviceProvider);
    }

Создать представление

@model ScrumApp.Models.UserProject

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>

            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>

            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Ответы [ 2 ]

1 голос
/ 13 января 2020

Попробуйте добавить внешние ключи к User и Project к классу UserProject.

public class UserProject
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int UserId { get; set; }
    public int ProjectId { get; set; }

    [ForeignKey("UserId")]
    public User User { get; set; }

    [ForeignKey("ProjectId")]
    public Project Project { get; set; }
}

Затем вы можете добавить объекты:

var userProject = new UserProject { UserId=.., ProjectId=.. };
context.UserProjects.Add(userProject);
0 голосов
/ 13 января 2020

Проблема в том, что Projects изначально нулевой. Вам нужно сначала инициализировать его:

appUser.Projects ??= new List<UserProject>();
appUser.Projects.Add(userProject);

Или просто установить значение по умолчанию для свойства:

public ICollection<UserProject> Projects { get; set; } = new List<UserProject>();

Работает хотя бы один проект (и включает в себя отношение в запросе) вокруг вопроса, поскольку EF уже создал экземпляр коллекции. Однако это не решает проблему во всех сценариях ios и не является «решением». Вам нужно правильно планировать и обрабатывать ноль.

...