EntityFramework adding nested entities fast - entity-framework

I have these models in the database. And I have a collection of POCOs representing these models and the relationships. I need to add them to the database and create the relationships between them. All objects count is like 200k. What is the most efficient way to do this ?
public class A
{
private ICollection<B> children;
public A()
{
this.children = new HashSet<B>();
}
public int Id { get; set; }
public virtual ICollection<B> Children
{
get { return this.children; }
set { this.children = value; }
}
}
public class B
{
private ICollection<C> children;
public B()
{
this.children = new HashSet<C>();
}
public int Id { get; set; }
public int AId { get; set; }
public virtual A A { get; set; }
public virtual ICollection<C> Children
{
get { return this.children; }
set { this.children = value; }
}
}
public class C
{
private ICollection<D> children;
public C()
{
this.children = new HashSet<D>();
}
public int Id { get; set; }
public int BId { get; set; }
public virtual B B { get; set; }
public virtual ICollection<D> Children
{
get { return this.children; }
set { this.children = value; }
}
}
public class D
{
private ICollection<E> children;
public D()
{
this.children = new HashSet<E>();
}
public int Id { get; set; }
public int CId { get; set; }
public virtual C C { get; set; }
public virtual ICollection<E> Children
{
get { return this.children; }
set { this.children = value; }
}
}
public class E
{
public int Id { get; set; }
public int DId { get; set; }
public virtual D D { get; set; }
}

If you mean the fastest way, use raw ADO.NET, even consider using SqlBulkCopy.
You could also consider using TVPs with raw ADO.NET rather than BULK COPY

Related

Failing to pass a complex object from one page to another in .Net Maui

I am trying to pass a complex object from MainPage to a ProductsPage, the object is a model with 4 class lists. Of the 4 class lists only 2 are passing data to the ProductsPage, the other 2 are not. I dont know where i am going wrong, i am using MVVM
My MainPageViewModel is as below
public partial class MainPageViewModel : BaseViewModel
{
public ObservableCollection<LogInModel> LogInModels { get; } = new();
public MainPageViewModel()
{
}
[ObservableProperty]
LogInModel logInModel;
[RelayCommand]
async Task GoToRetailAsync()
{
if (LogInModels.Count != 0)
LogInModels.Clear();
LogInModels.Add(logInModel);
await Shell.Current.GoToAsync($"{nameof(ProductsPage)}", true,
new Dictionary<string, object>
{
{"shiptoo",LogInModels[0].cat },
{"group",LogInModels[0].grp },
{"products",LogInModels[0].prod },
{"shipto",LogInModels[0].shp }
});
}
}
}
the failing class lists are shiptoo and group
Below is my ProductsViewModel
namespace Tenga.ViewModel
{
[QueryProperty("Products", "products")]
[QueryProperty("Group","group")]
[QueryProperty("Shiptoo","shiptoo")]
[QueryProperty("Shipto", "shipto")]
public partial class ProductsViewModel : BaseViewModel
{
public ProductsViewModel()
{
}
[ObservableProperty]
List<Shiptoo> shppp;
[ObservableProperty]
List<Group> groups;
[ObservableProperty]
List<Products> products;
[ObservableProperty]
List<Shipto> shipto;
}
}
Below is my LogInModel
namespace Tenga.Model
{
public class LogInModel
{
public string OTP { get; set; }
public string CustomerNumber { get; set; }
public string CustomerName { get; set; }
public string Balance { get; set; }
public string OpenToBuy { get; set; }
public string CreditLimit { get; set; }
public string LoginStatus { get; set; }
public string Error { get; set; }
public List<Shipto> shp = new List<Shipto>();
public List<Shiptoo> cat = new List<Shiptoo>();
public List<Group> grp = new List<Group>();
public List<Products> prod = new List<Products>();
}
public class Shipto
{
public string ShipCode { get; set; }
public string ShipDescription { get; set; }
}
public class Products
{
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
public string UOM { get; set; }
public string ConversionFactor { get; set; }
public string Category { get; set; }
public string Group { get; set; }
public byte[] Image { get; set; }
}
public class Shiptoo
{
public string ShipCode { get; set; }
public byte[] Image { get; set; }
}
public class Group
{
public string Category { get; set; }
public string Code { get; set; }
public byte[] Image { get; set; }
}
}
I have tried to review the class all seems alright, i have also tried changing the bindings and result is the same, can some one please help before go crazy
Implement IQueryAttributable in your ViewModel.
And use:
public void ApplyQueryAttributes(IDictionary<string, object> query)
{
Model = query[nameof(MyModel )] as MyModel ;
}
Forget about those annotations. This is better. You cant mistake names, you can run code after/before they are set. I migrated all my code to use this.
Edit: While we are on the subject:
Instead of:
{"shiptoo",LogInModels[0].cat },
You should be using some constants. The name of the model usually. (Something like naming conventions when passing Extras in android, but much more simple).

Map an Entity iEnumerator To Dto Enumerator

I am using CQRS. I select my Entities IEnumerator from database and i want to map this to my Dto class.
My Dto class:
public class XCollectionDto
{
public IEnumerable<XReadDto> Entries { get; init; } = Enumerable.Empty<XReadDto>();
}
My mapper class:
public class XReadMapper : IEntityToDtoMapper<X, XCollectionDto>
{
public XCollectionDto Map(IEnumerable <X> source, XCollectionDto target)
{
//todo
Here i want to map source to target Entries list
}
}
How can i do that, without a for loop? I am not using AutoMaper, the mapping is manual
I think you could accompish your purpose with C# reflection
I created the two class for test:
public class somemodel
{
public int Id { get; set; }
public string Name { get; set; }
public List<int> Numlist { get; set; }
}
public class somemodelDTO
{
public int Id { get; set; }
public string SomeName { get; set; }
public List<int> Numlist { get; set; }
}
the method to bind properties of somemodelDTO which have the same name with properties of somemodel:
private static somemodelDTO GetMap<somemodel, somemodelDTO>(somemodel some)
{
somemodelDTO somemDTO = Activator.CreateInstance<somemodelDTO>();
var typesource = some.GetType();
var typedestination = typeof(somemodelDTO);
foreach(var sp in typesource.GetProperties())
{
foreach( var dp in typedestination.GetProperties())
{
if(sp.Name==dp.Name)
{
dp.SetValue(somemDTO, sp.GetValue(some, null), null);
}
}
}
return somemDTO;
}
The result:

Entity Framework always adds two records in the tables

I'm implementing an ASP.NET Core 3.1 app. I have implemented following code to insert record in SQL Server database via EF Core but each time I save data, it inserts two records in PersonRequester and Requester table. I appreciate if anyone suggests me how I can prevent reinserting records.
Requester ap = new Requester();
ap.Address = RequesterViewModel.Requestervm.Address;
ap.RequesterType = RequesterViewModel.Requestervm.RequesterType;
ap.Description = RequesterViewModel.Requestervm.Description;
ap.Name = RequesterViewModel.Requestervm.Name;
var pa = new PersonRequester()
{
BirthCertificateNo = RequesterViewModel.personRequestervm.BirthCertificateNo,
IssuePlace = RequesterViewModel.personRequestervm.IssuePlace,
NationalCode = RequesterViewModel.personRequestervm.NationalCode,
Requester = ap
};
using (var context = new DBContext())
{
context.PersonRequester.Attach(pa);
try
{
context.SaveChanges();
}
catch (Exception e)
{
throw e;
}
}
public partial class Requester
{
public Requester()
{
PersonRequester = new HashSet<PersonRequester>();
}
public int RequesterId { get; set; }
public int RequesterType { get; set; }
public string Address { get; set; }
public string Description { get; set; }
public string Name { get; set; }
public virtual EntityType RequesterTypeNavigation { get; set; }
public virtual ICollection<PersonRequester> PersonRequester { get; set; }
}
public partial class PersonRequester
{
public int RequesterId { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public int RequesterType { get; set; }
public string NationalCode { get; set; }
public string BirthCertificateNo { get; set; }
public string IssuePlace { get; set; }
public virtual Requester Requester { get; set; }
public virtual EntityType RequesterTypeNavigation { get; set; }
}

Entity Framework mapping 2 one to many with existing DB and Model

I have been trying to update a project of my with Entity Framework. I have an existing database and model, which after some work matched. With some more work, I managed to get the application to successfully read the data from the database. But when saving new data, the nightmare started.
In my data structure, I have 2 one-to-many relations, one between player and team and one between match and team. I have been trying many configurations (with [Key], foreign key, inverse property attributes) but I either get an error
Trying to cast List to team
or
no column found Match_id no column found Player_id
This is my code:
public class Match
{
//[Key]
//[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string Id { get; set; }
//[Column("Date")]
public DateTime Date { get; set; }
public DateTime EndTime { get; set; }
[NotMapped]
public bool Remove { get; set; }
//[InverseProperty("Match")]
[ForeignKey("MatchId")]
public virtual ICollection<Team> Teams { get; set; }
public int WinningTeamId { get; set; }
public MatchType MatchType { get; set; }
public string Source { get; internal set; }
public Match()
{
WinningTeamId = -1;
this.Teams = new List<Team>();
this.EndTime = (DateTime)SqlDateTime.MinValue;
}
}
public class Team
{
public int TeamId { get; set; }
[NotMapped]
public int RatingChange { get; set; }
[Key()]
[Column("PlayerId", Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public virtual Player Player { get; set; }
[Column(Order = 1)]
[Key()]
//[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string MatchId { get; set; }
[ForeignKey("MatchId")]
public virtual Match Match { get; set; }
[NotMapped]
public int Rating { get; set; }
public Team()
{
}
}
public class Player
{
public string name { get; set; }
public int PastatsId { get; set; }
public string UberId { get; set; }
//[ForeignKey("PlayerId")]
//[InverseProperty("Player")]
public virtual ICollection<Team> Teams { get; set; }
//[Key()]
public int Id { get; set; }
}
To save the new matches, I first save the players to prevent conflicts there:
public void Save(IEnumerable<Match> matches)
{
foreach (var match in matches)
{
foreach (var team in match.Teams)
{
var entry = Entry(team.Player);
if (entry.State == EntityState.Detached)
{
var localplayer = Players.Local.FirstOrDefault(x => x.UberId == team.Player.UberId);
if (localplayer == null)
{
this.Players.Add(team.Player);
team.Player = entry.Entity;
}
else
{
team.Player = localplayer;
}
}
else
{
Entry(team.Player).State = EntityState.Modified;
}
}
}
SaveChanges();
foreach (var match in matches)
{
if (Matches.Find(match.Id) != null)
{
continue;
}
if (Entry(match).State == EntityState.Detached)
{
this.Matches.Add(match);
}
}
SaveChanges();
}
How to do this mapping with an existing database and code?
Any insight will be gratefully appreciated.

EF: validation error for 1:0..1 relationship in data model with navigation properties

I have this simple data model of some reservations and theirs cancellations:
[Table("ReservationCreation")]
public class ReservationCreation
{
[Key()]
public int ReservationCreationId { get; set; }
[InverseProperty("ReservationCreation")]
public virtual ReservationCancellation ReservationCancellation { get; set; }
}
[Table("ReservationCancellation")]
public class ReservationCancellation
{
[Key()]
[ForeignKey("ReservationCreation")]
public int ReservationCancellationId { get; set; }
[Required]
[ForeignKey("ReservationCancellationId")]
[InverseProperty("ReservationCancellation")]
public virtual ReservationCreation ReservationCreation { get; set; }
}
public class DbContext : System.Data.Entity.DbContext
{
public DbContext() : base(#"DefaultConnection") { }
public DbSet<ReservationCancellation> ReservationCancellation { get; set; }
public DbSet<ReservationCreation> ReservationCreation { get; set; }
}
internal sealed class Configuration : DbMigrationsConfiguration<DbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
}
Here is the code of the test. First the reservation is created and then it is cancelled.
When the cancellation record is being saved into database then an exception is thrown "The ReservationCreation field is required".
How can I create cancellation record only from the reservation's ID and at the same time have the navigation properties defined?
class Program
{
static void Main(string[] args)
{
int reservationId;
// create reservation
using (var db = new DbContext())
{
var reservation =
db.ReservationCreation.Add(
new ReservationCreation());
db.SaveChanges();
reservationId = reservation.ReservationCreationId;
}
// cancel reservation by its Id
using (var db = new DbContext())
{
var cancellation =
db.ReservationCancellation.Add(
new ReservationCancellation
{
ReservationCancellationId = reservationId
});
try
{
// an exception is thrown
db.SaveChanges();
}
catch(DbEntityValidationException ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
foreach (var err in ex.EntityValidationErrors.SelectMany(x_ => x_.ValidationErrors))
System.Diagnostics.Debug.WriteLine($"!!!ERROR!!! {err.PropertyName}: {err.ErrorMessage}");
}
}
}
}
I did not find any way how to modify the data model annotations. If I remove [Required] from ReservationCreation property then I am not able to create the migration {or connect to the database with that data model).
Your mixing things up in your ReservationCancellation model.
In your ReservationCreation property you are referring to the primary key entity instead of the ReservationCreation property.
Try this.
[Table("ReservationCancellation")]
public class ReservationCancellation
{
[Key()]
public int ReservationCancellationId { get; set; }
[ForeignKey("ReservationCreation")]
public int ReservationCreationId { get; set; }
[Required]
public virtual ReservationCreation ReservationCreation { get; set; }
}
Update
Since you want only one cancellation per creation, you can do this using a simpler model.
[Table("ReservationCreation")]
public class ReservationCreation
{
[Key()]
public int ReservationCreationId { get; set; }
public virtual ReservationCancellation ReservationCancellation { get; set; }
}
[Table("ReservationCancellation")]
public class ReservationCancellation
{
[Key()]
public int ReservationCancellationId { get; set; }
public virtual ReservationCreation ReservationCreation { get; set; }
}
I followed the recommendations from #dknaack and my final solution of this problem is this data model:
[Table("ReservationCreation")]
public class ReservationCreation
{
[Key()]
public int ReservationCreationId { get; set; }
[InverseProperty("ReservationCreation")]
public virtual ReservationCancellation ReservationCancellation { get; set; }
}
[Table("ReservationCancellation")]
public class ReservationCancellation
{
[Key()]
[ForeignKey("ReservationCreation")]
public int ReservationCancellationId { get; set; }
[ForeignKey("ReservationCancellationId")]
public virtual ReservationCreation ReservationCreation { get; set; }
}