When the entity framework receives the input in form of a post request from soap ui, it should insert the data record or multiple data records(rows) into the BdpMigrationList table and return a confirmation. BdpMigrationList was added to entity using sccafold command.
Model:
public class BDPHMMigModel
{
public string CmNo { get; set; }
public DateTime CmDate { get; set; }
public string Ipact { get; set; }
public string Site { get; set; }
public string Version { get; set; }
public string Circuit { get; set; }
public string Customer { get; set; }
public string SourceDevice { get; set; }
public string SourceInterface { get; set; }
public string SourceInterfaceLag { get; set; }
public string DestDevice { get; set; }
public string DestInterface { get; set; }
public string DestInterfaceLag { get; set; }
public int Id { get; set; }
}
Here is my Data Access layer code: Please also recommend which class type should I use in the DAL code.
public async Task<int> AddBdpHmMigData(BdpMigrationList migrationList)
{
var context = new IpReservationContext();
object newcircuit = new BdpMigrationList
{
CmNo = migrationList.CmNo,
CmDate = migrationList.CmDate,
Ipact = migrationList.Ipact,
Site = migrationList.Site,
Version = migrationList.Version,
Circuit = migrationList.Circuit,
Customer = migrationList.Customer,
SourceDevice = migrationList.SourceDevice,
SourceInterface = migrationList.SourceInterface,
SourceInterfaceLag = migrationList.SourceInterfaceLag,
DestDevice = migrationList.DestDevice,
DestInterface = migrationList.DestInterface,
DestInterfaceLag = migrationList.DestInterfaceLag
};
context.Add(newcircuit);
context.SaveChanges();
}
Controller:
[HttpPost]
public async Task<IActionResult> AddBdpHmMigData([FromBody]BDPHMMigModel model)
{
if (ModelState.IsValid)
{
try
{
var miglistID = await BdpHmDao.AddBdpHmMigData(model);
if (miglistID > 0)
{
return Ok(miglistID);
}
else
{
return NotFound();
}
}
catch (Exception)
{
return BadRequest();
}
}
return BadRequest();
BdpMigrationList class in entity:
[Table("BDP_Migration_List")]
public partial class BdpMigrationList
{
[Required]
[Column("CM_No")]
[StringLength(50)]
public string CmNo { get; set; }
[Column("CM_date", TypeName = "datetime")]
public DateTime CmDate { get; set; }
[Column("IPACT")]
[StringLength(50)]
public string Ipact { get; set; }
[StringLength(10)]
public string Site { get; set; }
[StringLength(10)]
public string Version { get; set; }
[Required]
[StringLength(60)]
public string Circuit { get; set; }
[StringLength(150)]
public string Customer { get; set; }
[Column("Source_Device")]
[StringLength(50)]
public string SourceDevice { get; set; }
[Column("Source_Interface")]
[StringLength(50)]
public string SourceInterface { get; set; }
[Column("Source_Interface_Lag")]
[StringLength(50)]
public string SourceInterfaceLag { get; set; }
[Column("Dest_Device")]
[StringLength(50)]
public string DestDevice { get; set; }
[Column("Dest_Interface")]
[StringLength(50)]
public string DestInterface { get; set; }
[Column("Dest_Interface_Lag")]
[StringLength(50)]
public string DestInterfaceLag { get; set; }
[Key]
[Column("ID")]
public int Id { get; set; }
}
dbcontext:
public partial class IpReservationContext : DbContext
{
public IpReservationContext()
{
}
public IpReservationContext(DbContextOptions<IpReservationContext> options)
: base(options)
{
}
public virtual DbSet<BdpMigrationList> BdpMigrationList { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BdpMigrationList>(entity =>
{
entity.Property(e => e.Circuit).IsUnicode(false);
entity.Property(e => e.CmNo).IsUnicode(false);
entity.Property(e => e.Customer).IsUnicode(false);
entity.Property(e => e.DestDevice).IsUnicode(false);
entity.Property(e => e.DestInterface).IsUnicode(false);
entity.Property(e => e.DestInterfaceLag).IsUnicode(false);
entity.Property(e => e.Ipact).IsUnicode(false);
entity.Property(e => e.Site).IsUnicode(false);
entity.Property(e => e.SourceDevice).IsUnicode(false);
entity.Property(e => e.SourceInterface).IsUnicode(false);
entity.Property(e => e.SourceInterfaceLag).IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
getting the following errors:
Error CS1503 Argument 1: cannot convert from 'homing_matrix_api.Model.BDPHMMigModel' to 'homing_matrix_api.Entity.IpReservation.BdpMigrationList' homing-matrix-api C:\Repositories\HomingMatrix\HomingMatrixAPI\Controllers\BdpHmController.cs 55 Active
Error CS0161 'BdpHmDao.AddBdpHmMigData(BdpMigrationList)': not all code paths return a value homing-matrix-api C:\Repositories\HomingMatrix\HomingMatrixAPI\Data\BdpHmDao.cs 51 Active
Warning CS1998 This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread. homing-matrix-api C:\Repositories\HomingMatrix\HomingMatrixAPI\Data\BdpHmDao.cs 51 Active
Try this code:
Fix your controller code:
using ... Data Access layer name space
....
......
var bdp=new bdpHmDao();
var miglistID = await bdp.AddBdpHmMigData(model);
Fix your action code
public async Task<int> AddBdpHmMigData(BDPHMMigModel model)
{
using (var context = new IpReservationContext())
{
var newcircuit = BdpMigrationList
{
CmNo = model.CmNo,
CmDate = model.CmDate,
Ipact = model.Ipact,
Site = model.Site,
Version = model.Version,
Circuit = model.Circuit,
Customer = model.Customer,
SourceDevice = model.SourceDevice,
SourceInterface = model.SourceInterface,
SourceInterfaceLag = model.SourceInterfaceLag,
DestDevice = model.DestDevice,
DestInterface = model.DestInterface,
DestInterfaceLag = model.DestInterfaceLag
};
context.Set<BdpMigrationList>().Add(newcircuit);
return await context.SaveChangesAsync();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.