简体   繁体   English

如何使用Postgresql将值添加到2个表,然后将外键从一个表映射到Entity Framework Core中的另一个表?

[英]How to add values to 2 table then map foreign key from one table to another table in Entity Framework Core using Postgresql?

Here is another issue I have faced in ASP.NET Core Let say I have 2 Tables: 这是我在ASP.NET Core中遇到的另一个问题,可以说我有2个表:

Accommodation
( ID, Name, LocationID)
Location
(ID, Address, Latitude, Longitude)

And I have a form: 我有一个表格:

Name:
Address:
Latitude:
Longitude:

Then when a button is clicked I want the value to be updated on both table and also MAP the LocationID to Accommodation table How should I code it in EF Core? 然后,当单击一个按钮时,我要在两个表上同时更新该值,还要将LocationID映射到Accommodation表中。如何在EF Core中对其进行编码? Like a proper way 像正确的方法

You could try the following example I made : 您可以尝试下面的示例:

Accommodation Model and Location Model 住宿模型和位置模型

 public class Accommodations
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int LocationID { get; set; }
    [ForeignKey("LocationID")]
    public Location Location { get; set; }
}
public class Location
{
    public int ID { get; set; }
    public string Address { get; set; }
    //other stuff you want
}

DbSet two model in DbContext DbSet在DbContext中的两个模型

 public class MVCDbContext:DbContext
{
    public MVCDbContext(DbContextOptions<MVCDbContext> options) : base(options)
    { }
    public DbSet<Accommodations> Accommodations { get; set; }
    public DbSet<Location> Location { get; set; }
}

The design of the view ,pay attention to the asp-for of the input tag 视图的设计,请注意输入标签的asp-for

@model MVC2_1Test.Models.Accommodation.Accommodations
@{
ViewData["Title"] = "Accommondation";
}

<h2>Accommondation</h2>

<div class="row">
<div class="col-md-4">
    <form id="form" class=".has-error" asp-action="CreateAccommodation">
        <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" id="cusName" />
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Location.Address" class="control-label"></label>
            <input asp-for="Location.Address" class="form-control" id="age" />
            <span asp-validation-for="Location.Address" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-default" />

        </div>
    </form>
</div>
</div>

The action to update both table 更新两个表的动作

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> CreateAccommodation([FromForm]Accommodations accommodation)
    {
        if (ModelState.IsValid)
        {
            _context.Add(accommodation);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
        return View(accommodation);
    }

暂无
暂无

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

相关问题 在主表上没有外键的一对多实体框架 - Entity Framework Core One To Many with No Foreign Key on Master Table 实体框架一个表中有两个外键 - Entity Framework Two foreign key in one table 如何使用实体框架c#显示与表中关联的外键的三个表的值? - how to show values of three table with foreign key associated in table using entity framework c#? 如何使用 Entity Framework Core 将 SQL 数据从一个表传输到另一个表 - How to transfer SQL data from one table to another using Entity Framework Core 如何将一个表中的主(代理)键添加到另一个表的外键中? - How to add a primary (Surrogate) key from one table into a foreign key of another table? 如何使用Entity Framework将多个对象添加到外表 - How to add multiple objects to a foreign table using Entity Framework 使用Entity Framework Core使用外键创建新表 - Creating new table with foreign key with Entity Framework Core 如何在 ASP.NET Core 6 Web API 中使用一对一关系将外键添加到表中? - How to add a foreign key into a table using one-to-one relationship in ASP.NET Core 6 Web API? 如何使用Entity Framework中另一个表上的外键引用的汇总值更新表的所有行 - How to update all rows of a table with an aggregate value referenced by foreign key on an another table in Entity Framework 如何使用 Entity Framework Core 从数据库表中确定通用实体的下一个主键? - How to determine the next primary key from a database table for a generic entity using Entity Framework Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM