For example, I have the following infrastructure class:
[Table("GeoHistory")]
public partial class GeoHistory
{
public int Id { get; set; }
public int CompanyId { get; set; }
public int DriverId { get; set; }
[StringLength(50)]
public string Name { get; set; }
public string Description { get; set; }
[StringLength(100)]
public string GeofenceLocation { get; set; }
[StringLength(100)]
public string GeofencePrevious { get; set; }
[StringLength(20)]
public string StateLocation { get; set; }
[StringLength(20)]
public string StatePrevious { get; set; }
public DateTime? DateTime { get; set; }
[StringLength(5)]
public string Heading { get; set; }
public decimal? Speed { get; set; }
[StringLength(50)]
public string Status { get; set; }
}
and the following View class (let's forget about domain layer):
public class GeoHistoryViewModel
{
public int Id { get; set; }
public int? CompanyId { get; set; }
public int? DriverId { get; set; }
[StringLength(50)]
public string Name { get; set; }
public string Description { get; set; }
}
as we can see, we edit only part of field list.
Now, we want to update data in DB. Of course, we can write like:
Infrastructure.Main.GeoHistory geoHistory = db.GeoHistories.Find(id);
geoHistory.CompanyId = model.CompanyId;
geoHistory.DriverId = model.DriverId;
geoHistory.Name = model.Name;
........
db.SaveChanges();
It works. But I want to use Automapper. And if I try to do the following:
Infrastructure.Main.GeoHistory geoHistory = mapper.Map<Infrastructure.Main.GeoHistory>(model);
db.GeoHistories.Attach(geoHistory);
db.Entry(geoHistory).State = EntityState.Modified;
db.SaveChanges();
It works, but of course remove values of fields, which are not exist in View Model, but exist in infrastructure class. How to use automapper, but don't lost these fields?
Related
I'm having the following situation: the Total from the Valid Nest Response is equals 2 and the Documents count equal 0. I'm not sure how that is possible.
Found the solution. The problem was in my model. You see, my model was something like that:
public class Customer
{
public string Id { get; set; }
public string Name { get; set; }
public string Nickname { get; set; }
public string PersonType { get; set; }
public string Gender { get; set; }
public string MaritalStatus { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy HH:mm:ss}")]
public DateTime? BirthDate { get; set; }
}
And this Index had Documents missing some fields, like:
public class Customer
{
public string Id { get; set; }
public string Name { get; set; }
public string PersonType { get; set; }
public string Gender { get; set; }
}
The solution was adding the decorator PropertyName from Nest, to Map my model, like this:
public class Customer
{
[PropertyName("id")]
public string Id { get; set; }
[PropertyName("name")]
public string Name { get; set; }
[PropertyName("nickname")]
public string Nickname { get; set; }
[PropertyName("personType")]
public string PersonType { get; set; }
[PropertyName("gender")]
public string Gender { get; set; }
[PropertyName("maritalStatus")]
public string MaritalStatus { get; set; }
[PropertyName("birthDate")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{dd/MM/yyyy HH:mm:ss}")]
public DateTime? BirthDate { get; set; }
}
Now it works!
I am trying to build a simple application where I can store and retrieve some details about some devices and the users of them, like an inventory. But when I try to display the list of devices with their owners, Automapper throws this error:
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
I don't understand what I am doing wrong here. How can I deal with this?
Startup.cs
builder.Services.AddAutoMapper(typeof(MapConfig));
builder.Services.AddControllersWithViews();
var app = builder.Build();
MapConfig.cs
public class MapConfig : Profile
{
public MapConfig()
{
CreateMap<Asset, AssetVM>().ReverseMap();
CreateMap<AppUser, AppUsersVM>().ReverseMap();
}
}
Asset.Cs
public class Asset
{
public int Id { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public string? ProductNumber { get; set; }
public string? SerialNumber { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public bool IsAssigned { get; set; }
public string? ISN { get; set; }
public string Status { get; set; }
public bool IsInsured { get; set; }
public string Condition { get; set; }
[ForeignKey("UserId")]
public AppUser AppUser { get; set; }
public string? UserId { get; set; }
}
AssetVM
public class AssetVM
{
public int Id { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
[Display(Name ="Product Number")]
public string? ProductNumber { get; set; }
[Display(Name ="Serial Number")]
public string? SerialNumber { get; set; }
[Display(Name ="Date Created")]
[DataType(DataType.Date)]
public DateTime DateCreated { get; set; }
[Display(Name = "Date Modified")]
[DataType(DataType.Date)]
public DateTime DateModified { get; set; }
[Display(Name ="Assigned")]
public bool IsAssigned { get; set; }
public string? ISN { get; set; }
[Required]
public string Status { get; set; }
[Display(Name ="Has Insurance")]
public bool IsInsured { get; set; }
[Required]
public string Condition { get; set; }
public string? UserId { get; set; }
public SelectList? AppUsersList { get; set; }
public AppUsersVM AppUsers { get; set; }
}
This is how I get and map the data that is to be displayed on the page:
public async Task<AssetVM> GetAssets()
{
var asset = await context.Assets.Include(q => q.AppUser).ToListAsync();
var model = mapper.Map<AssetVM>(asset);
return model;
}
And finally I return the result of GetAssets method to the view in my controller:
var model = await assetRepository.GetAssets();
return View(model);
Well, I found what I'm doing wrong. This is what I've done:
Since I was getting a list after querying the database in my GetAssets method, I had to change my mapping to:
var model = mapper.Map<List<AssetVM>>(asset);
And to be able to return this model, I also had to change my method declaration to:
public async Task<List<AssetVM>> GetAssets()
This changes made it work, except I didn't get the details of the user that is using that asset. And this was due to a typo in my AssetVM viewmodel.
public AppUsersVM AppUser { get; set; }
Those were all the changed I had to do. Still have a looong way to be a competent programmer so I'd be glad if you let me know if I have any flaws in my logic or any recommendations at all.
I will appreciate if somebody can tell me why entity framework is not creating join table for following model. It is creating table for type and feature but not the table that will join them.
public class DeviceType
{
[Display(Name = "ID")]
public int DeviceTypeID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public IEnumerable<DeviceFeature> DeviceFeatures { get; set; }
}
public class DeviceFeature
{
[Display(Name = "ID")]
public int DeviceFeatureID { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public IEnumerable<DeviceType> DeviceTypes { get; set; }
}
public class DeviceFeatureView
{
public virtual IEnumerable<DeviceType> DeviceTypes { get; set; }
public virtual IEnumerable<DeviceFeature> DeviceFeatures { get; set;
}
You do not need the bridge to create a many-to-many relationship. EF will figure it out. Change the type of the navigation properties from IEnumerable to ICollection like this:
public class DeviceType
{
public DeviceType()
{
this.DeviceFeatures = new HashSet<DeviceFeature>();
}
[Display(Name = "ID")]
public int DeviceTypeID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<DeviceFeature> DeviceFeatures { get; set; }
}
public class DeviceFeature
{
public DeviceFeature()
{
this.DeviceTypes = new HashSet<DeviceType>();
}
[Display(Name = "ID")]
public int DeviceFeatureID { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public ICollection<DeviceType> DeviceTypes { get; set; }
}
More about it here.
I'm using servicestack, ormlite, and mysql and Have linked table that are created within a transaction. Using the connection Save method, it should (according to documentation) update the model with the identity value. But it does not.
public bool SaveWithCredentials<T>(T model)
{
SetDefaultValues(false, model);
var ret = Db.Save(model);
return ret;
}
(EDITED WITH MODEL)
The model:
[Alias("orderEvent")]
public class OrderEvent
{
[AutoIncrement]
[Alias("Identity")]
public long Identity { get; set; }
[Required]
[StringLength(DbConstraints.STRING_36, DbConstraints.STRING_36)]
[Alias("Id")]
public string Id { get; set; }
[Required]
[StringLength(DbConstraints.STRING_36, DbConstraints.STRING_36)]
[Alias("orderId")]
public string OrderId { get; set; }
[Required]
[Alias("meaning")]
public int Meaning { get; set; }
[Required]
[Alias("subMeaning")]
public int SubMeaning { get; set; }
[Alias("quantity")]
public int Quantity { get; set; }
[Required]
[Alias("type")]
public int Type { get; set; }
[StringLength(DbConstraints.STRING_36, DbConstraints.STRING_36)]
[Alias("servicesaleId")]
public string ServiceSaleId { get; set; }
[Required]
[StringLength(DbConstraints.STRING_36, DbConstraints.STRING_36)]
[Alias("servicecenterId")]
public string ServiceCenterId { get; set; }
[StringLength(DbConstraints.STRING_36, DbConstraints.STRING_36)]
[Alias("projectId")]
public string ProjectId { get; set; }
[Alias("ecoDate")]
public DateTime EcoDate { get; set; }
[Alias("createdDate")]
public DateTime CreatedDate { get; set; }
[StringLength(DbConstraints.STRING_50)]
[Alias("createdByName")]
public string CreatedByName { get; set; }
[StringLength(DbConstraints.STRING_36, DbConstraints.STRING_36)]
[Alias("createdbyuserId")]
public string CreatedByUserId { get; set; }
[StringLength(DbConstraints.STRING_2000)]
[Alias("generalDescription")]
public string GeneralDescription { get; set; }
[Required]
[StringLength(DbConstraints.STRING_50)]
[Alias("sysecoactionId")]
public string SysEcoActionId { get; set; }
[StringLength(50)]
[Alias("sysOrderSerie")]
public string SysOrderSerie { get; set; }
[Alias("orderNbr")]
public int? OrderNbr { get; set; }
[Alias("itemNbr")]
public int? ItemNbr { get; set; }
}
The IDBConnection.Save should update the field with the autoincrement identity in the model so that I can set the foreign key in the linked table, but it does not.
Does anyone have any idea? I cannot do the insert method, since it would have me doing major rewrite - which sort of negates the purpose of the Save method!
UPDATE
Tried the model in a test method with insert method and I get the identity returned then.
public long InsertForIdWithCredentials<T>(T model)
{
SetDefaultValues(false, model);
var ret = Db.Insert(model, selectIdentity: true);
return ret;
}
the above code have no problem with returning identity even if it is generic.
/Erik
I am completely new to ASP.NET and was trying to cross-reference two different models with each other. I have 'Customer':
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
and 'Reservation':
public class Reservation
{
public int ReservationID { get; set; }
public string PetName { get; set; }
public DateTime Birthdate { get; set; }
public string Specie { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public Customer Customer { get; set; }
}
Now, each Reservation should belong to a Customer (as you can see in the bottom line of the Reservation model) and should therefore contain the CustomerID. On the other hand should each Customer contain references to each Reservation that was made by him.
How can I setup this relation?
Use an ICollection for a one to many relationship.
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public ICollection<Reservation> Reservations { get; set; }
}
Note I originally suggested a generic list, but there's a few subtle differences.