Return entity with sub table collection - entity-framework

Here is what I have:
public class StudentHealthInfoType
{
public int StudentHealthInfoId { get; set; }
public bool? HasAllergies { get; set; }
public List<HealthInfoMedicationType> HealthInfoMedicationType { get; set;}
}
public class HealthInfoMedicationType
{
public int HealthInfoMedicationId { get; set; }
public string MedicationName { get; set; }
}
var result = (from u in context.StudentHealthInfos
from m in context.HealthInfoMedications
where u.RegistrationId == registrationId
&& u.StudentHealthInfoId == m.StudentHealthInfoId
select new StudentHealthInfoType
{ StudentHealthInfoId = u.StudentHealthInfoId,
HasAllergies = u.HasAllergies, HealthInfoMedicationType = new HealthInfoMedicationType
{ HealthInfoMedicationId = m.HealthInfoMedicationId,
MedicationName = m.MedicationName
}
}).FirstOrDefault();
I get this error which is show at HealthInfoMedicationType = new HealthInfoMedicationType
Cannot implicitly convert type
'Dis.QueryManager.HealthFormTypes.HealthInfoMedicationType' to
'System.Collections.Generic.List'
HealthInfoMedicationType needs to be a collection of items returned for one StudentHealthInfoType record. How do I need to setup my objects and then cast them so this query works?

Please try this if you want to query for a particular registration id and list of HealthInfoMedications you can do like this .
var result = context.StudentHealthInfos
.Include(x=>x.HealthInfoMedicationType).FirstOrDefault(f => f.RegistrationId == 1);
// Result will be StudentHealthInfoType with list of HealthInfoMedicationType

Related

How to insert payload data in many-to-many relationships with EF Core 5

I have this relationship between Licitadores and Ofertas
public class Licitador
{
public int Id { get; set; }
public string Nombre { get; set; }
[StringLength(maximumLength: 15)]
public string CodigoSAP { get; set; }
public List<Oferta> Ofertas { get; set; } = new List<Oferta>();
}
public class Oferta
{
[StringLength(maximumLength:6)]
public string Id { get; set; }
[StringLength(maximumLength: 5)]
public string IdPresentada { get; set; }
....
public List<Licitador> Licitadores { get; set; } = new List<Licitador>();
}
And the join table in the context
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<LicitacionesEnSolitario>().ToTable("LicitacionesSolitario");
modelBuilder.Entity<Licitador>()
.HasMany(o => o.Ofertas)
.WithMany(of => of.Licitadores)
.UsingEntity<LicitacionesEnSolitario>
(oo => oo.HasOne<Oferta>().WithMany(),
oo => oo.HasOne<Licitador>().WithMany())
.Property(oo => oo.Adjudicado)
.IsRequired();
}
I need this data in my entity/table LicitacionesEnSolitario in addition to PK y FK
public class LicitacionesEnSolitario
{
public int LicitadorId { get; set; }
public string OfertaId { get; set; }
public bool Adjudicado { get; set; }
public string Plazo { get; set; }
public decimal PresupuestoOfertado { get; set; }
public DateTime? FechaAdjudicacion { get; set; }
}
Here I insert the data importing them from another database
public int ImportarLicitacionesEnSolitario()
{
try
{
int registrosAñadidos = 0;
var registrosSAP = _contextSAP.LicitacionesEnSolitario
.FromSqlRaw("sql")
.ToList();
foreach (var registroSAP in registrosSAP)
{
var oferta = _contextBoletus.Ofertas.Find(registroSAP.OfertaId);
var licitador = _contextBoletus.Licitadores.Where(l => l.CodigoSAP == registroSAP.CodigoSAP).FirstOrDefault();
oferta.Licitadores.Add(licitador);
registrosAñadidos +=1;
}
_contextBoletus.SaveChanges();
return registrosAñadidos;
}
catch (Exception ex)
{
throw ex;
}
}
This works fine and insert data in "LicitacionesEnSolitario" but with this fields Adjudicado, Plazo, PresupuestoPfertado y FechaAdjudicacion with nulls.
I don't know how to insert them at the time I insert Licitadores and if I try to update after the Add method using the PKs I just added
foreach (var registroSAP in registrosSAP)
{
var oferta = _contextBoletus.Ofertas.Find(registroSAP.OfertaId);
var licitador = _contextBoletus.Licitadores.Where(l => l.CodigoSAP == registroSAP.CodigoSAP).FirstOrDefault();
oferta.Licitadores.Add(licitador);
var ls = _contextBoletus.Set<LicitacionesEnSolitario>()
.SingleOrDefault(ls => ls.OfertaId == oferta.Id & ls.LicitadorId == licitador.Id);
ls.Adjudicado = registroSAP.Adjudicado;
ls.PresupuestoOfertado = registroSAP.PresupuestoOfertado;
ls.FechaAdjudicacion = registroSAP.FechaAdjudicacion;
registrosAñadidos +=1;
}
_contextBoletus.SaveChanges();
return registrosAñadidos;
I get this error System.NullReferenceException: Object reference not set to an instance of an object.
Any idea, please?
Thanks
This is the best way I found
foreach (var registroSAP in registrosSAP)
{
var oferta = _contextBoletus.Ofertas.Find(registroSAP.OfertaId);
var licitador = _contextBoletus.Licitadores.Where(l => l.CodigoSAP == registroSAP.CodigoSAP).FirstOrDefault();
var ls = _contextBoletus.Set<LicitacionesEnSolitario>().Add(
new LicitacionesEnSolitario
{
LicitadorId = licitador.Id,
OfertaId = oferta.Id,
Adjudicado = registroSAP.Adjudicado,
Plazo = registroSAP.Plazo,
PresupuestoOfertado = registroSAP.PresupuestoOfertado,
FechaAdjudicacion = registroSAP.FechaAdjudicacion
});
registrosAñadidos += 1;
}
Thanks

Web API Get with query parameters on Mongodb collection

I have WebAPI written and it is using mongodb collection as a database.
it looks like
[
{
"Id":"5a449c148b021b5fb4cb1f66",
"airline":[
{
"airlineID":-1,
"airlineName":"Unknown",
"airlineAlias":"",
"airlineIATACode":"-",
"airlineICAOCode":"N/A",
"airlineCallsign":"",
"airlineBaseCountry":"",
"airlineActiveIndicator":"Y"
},
{
"airlineID":1,
"airlineName":"Private flight",
"airlineAlias":"",
"airlineIATACode":"1T",
"airlineICAOCode":"N/A",
"airlineCallsign":"",
"airlineBaseCountry":"",
"airlineActiveIndicator":"Y"
},
{
"airlineID":2,
"airlineName":"135 Airways",
"airlineAlias":"",
"airlineIATACode":"2T",
"airlineICAOCode":"GNL",
"airlineCallsign":"GENERAL",
"airlineBaseCountry":"United States",
"airlineActiveIndicator":"N"
}
]
}
]
I'm trying to get data using airlineIATACode attribute
public airlineModel Get(string i)
{
_collection = _db.GetCollection<airlineModel>("airline");
var res = Query<airlineModel>.EQ(p => p.airline[0].airlineIATACode, i);
return _collection.FindOne(res);
}
My controller implementation
public HttpResponseMessage Get(string IATAcode)
{
var result = objds.Get(IATAcode);
if (result != null)
return Request.CreateResponse(HttpStatusCode.OK, result);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Data not found");
}
My Model class:
public class airlineModel
{
public ObjectId Id { get; set; }
[BsonElement("airline")]
public List<airlinedata> airline { get; set; }
}
public class airlinedata
{
[BsonElement("airlineID")]
public int airlineID { get; set; }
[BsonElement("airlineName")]
public string airlineName { get; set; }
[BsonElement("airlineAlias")]
public string airlineAlias { get; set; }
[BsonElement("airlineIATACode")]
public string airlineIATACode { get; set; }
[BsonElement("airlineICAOCode")]
public string airlineICAOCode { get; set; }
[BsonElement("airlineCallsign")]
public string airlineCallsign { get; set; }
[BsonElement("airlineBaseCountry")]
public string airlineBaseCountry { get; set; }
[BsonElement("airlineActiveIndicator")]
public string airlineActiveIndicator { get; set; }
}
When I run app and browse http://localhost:60387/api/airlineAPI?IATAcode=1T
it says, Data not found
What can I do to solve this problem?
You can try this.
public airlinedata Get(string i)
{
var _collection = database.GetCollection<airlineModel>("airline");
var filter = Builders<airlineModel>.Filter
.ElemMatch(model => model.airline, airline => airline.airlineIATACode == i);
var projection = Builders<airlineModel>.Projection
.Include(model => model.airline[-1]);
var airlineModel = _collection.Find(filter)
.Project<airlineModel>(projection)
.Single();
return airlineModel.airline.Single();
}
Note that you don't need to put mapping attributes on each field. The default mapping does exactly what you did with attributes.
The only attribute I suggest you to use is [BsonId] to tell MongoDB which is your _id field.
public class airlineModel
{
[BsonId(IdGenerator = typeof(ObjectIdGenerator))]
public ObjectId Id { get; set; }
...
}
Finally, in case of a large collection, don't forget to create an index on airlineIATACode field, otherwise the search would perform an expensive COLLSCAN.

How to insert data in a joined table with Entity framework?

I am new to Entity Framework and I am hoping for some help to insert data in a "joined table".
I have three tables, Profiles, Tags and one called ProfilesTags that joins these two tables. Classes are autogenerated from database / DB First.
public partial class Profiles
{
public Profiles()
{
this.ProfilesTags = new HashSet<ProfilesTags>();
}
public int ProfileId { get; set; }
public string Name { get; set; }
...
public virtual ICollection<ProfilesTags> ProfilesTags { get; set; }
}
public partial class Tags
{
public Tags()
{
this.ProfilesTags = new HashSet<ProfilesTags>();
}
public int TagId { get; set; }
public string Tag { get; set; }
public virtual ICollection<ProfilesTags> ProfilesTags { get; set; }
}
public partial class ProfilesTags
{
public int Id { get; set; }
public int ProfileId { get; set; }
public int TagId { get; set; }
public virtual Tags Tags { get; set; }
public virtual Profiles Profiles { get; set; }
}
I have a SaveTags method that looks like this:
public void SaveTags(int profileId, IEnumerable<TagsNameValue> tags)
{
var pr = Context.Profiles.First(p => p.ProfileId == profileId);
// remove any existing
pr.ProfilesTags.Clear();
if (tags == null || !tags.Any())
return;
var ids = tags.Select(value => value.Value);
var names = tags.Select(value => value.Name);
// get a list of tags for lookup from [Tags]-table
var tagsList = Context.Tags.Where(t => ids.Any(v => t.TagId == v) || names.Any(v => t.Tag == v)).ToList();
foreach (var nameValue in tags)
{
var tag = tagsList.FirstOrDefault(t => t.TagId == nameValue.Value || t.Tag.ToLower() == nameValue.Name.ToLower());
// Tag is already in [Tags], no need to recreate id, just associate it.
if (tag != null)
{
var tagModel = new ProfilesTags()
{
TagId = nameValue.Value,
ProfileId = profileId
};
pr.ProfilesTags.Add(tagModel);
}
// create new item in [Tags] table first and add association [ProfilesTags]
else
{
var newTag = new Tags { Tag = nameValue.Name};
// how do I associate this newly added tag to pr.ProfilesTags ?
// what to do / how to procede?
Context.Tags.Add(newTag);
}
}
Context.SaveChanges()
}
How can I associate newTag with pr.ProfilesTags?
It seems newTag should have a valid id first, then build the relationship by ProfilesTags later.
// create new item in [Tags] table first and add association [ProfilesTags]
else
{
var newTag = new Tags { Tag = nameValue.Name};
// how do I associate this newly added tag to pr.ProfilesTags ?
// what to do / how to procede?
Context.Tags.Add(newTag);
// Let newTag has a valid tagId
Context.SaveChanges();
// Build the relationship between `Profiles` and `Tags`.
var newProfileTag = new ProfilesTags();
/// Build the relationship by ForeignKey,
/// Or, call pr.ProfilesTags.Add(newProfileTag)
newProfileTag.ProfiledId = pr.ProfileId;
newProfileTag.TagId = newTag.TagId; //< It SHOULD NOT be zero...
Context.ProfilesTags.Add(newProfileTag);
// Save the newProfileTag.....
// Context.SaveChanges();
}

NullReference error when adding a new model into a model property

I have the following entity framework code first models:
public class Member {
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public String CardNumber { get; set; }
//Foreign Key
public virtual ICollection<Favorite> Favorites { get; set; }
[NotMapped]
public List<SelectListItem> FavoriteTypes { get; set; }
public Member() {
MembersDB db = new MembersDB();
FavoriteTypes = new List<SelectListItem>();
FavoriteTypes.AddRange(db.FavoriteTypes.ToList().Select(f => new SelectListItem { Text = f.Value, Value = f.ID.ToString() }));
}
}
public class FavoriteType {
public int ID { get; set; }
public string Value { get; set; }
}
public class Favorite {
public int ID { get; set; }
public String Value { get; set; }
//Foreign Keys
public virtual FavoriteType FavoriteType { get; set; }
public virtual Member Member { get; set; }
}
This creates a 1-M relationship for FavoriteTypes -> Favorites and 1-M relations for Member -> Favorites
Within my controller action, I retrieve most of the Member's info from Session saved at a couple pages back except for the favorites info which is gathered below. Then I gather the list of ID and input values to add to my new member as so:
[HttpPost]
public ActionResult AddFavs(List<int> ID, List<string> Value) {
MembersDB db = new MembersDB();
Member newMember = (Member)Session["member"];
if (ID != null && Value != null)
{
for (int i = 0; i < ID.Count(); i++)
{
int currentID = ID[i];
var test = new Favorite();
test.FavoriteType = db.FavoriteTypes.Where(f => f.ID == currentID).FirstOrDefault();
test.Value = Value[i];
newMember.Favorites.Add(test);
}
}
While running this code I get a NullReference error on this line newMember.Favorites.Add(test);
Not entirely sure why, any help would be appreciated.
EDIT: while troubleshooting in VS, the only null properties I can find are Favorites in newMember and Member in test
ICollection<Favorite> Favorites is null, so you can't add items to it. You should instantiate it in the constructor of your model:
public Member()
{
Favorites = new List<Favorite>();
// ...
}
Now it's an empty collection and you can add items to it.

Update list object based on other LINQ / LAMBDA

Here are my two objects
public class ObjectHeaderBuffer
{
public int DataObjectId { get; set; }
public string FileName { get; set; }
public int RowCount { get; set; }
public string Checksum { get; set; }
public int ReconTarget { get; set; }
}
public class ObjectHeaderAttribute
{
public int DataObjectId { get; set; }
public int AttributeType { get; set; }
public int AttributeValue { get; set; }
}
var ohBuffer = new List<ObjectHeaderBuffer>();
var ohAttribute = new List<ObjectHeaderAttribute>();
I want to update ohBuffer.ReconTarget with ohAttribute.AttributeValue where ohBuffer.DataObjectId == ohAttribute.DataObjectId
what is linq or lambda of this?
You need to iterate each item in ohBuffer and look up the value in ohAttribute.
Assuming there is only one Attribute for each Buffer, this will work.
ohBuffer.ForEach(b => b.ReconTarget = ohAttribute
.SingleOrDefault(a => a.DataObjectId == b.DataObjectId).AttributeValue);
If the lookup returns null, you can either coalesce to a new object and take the default value
ohBuffer.ForEach(b => b.ReconTarget =
(ohAttribute.SingleOrDefault(a => a.DataObjectId == b.DataObjectId)
?? new ObjectHeaderAttribute())
.AttributeValue);
or you could just take null
ohBuffer.ForEach(b => b.ReconTarget =
{
var attribute = ohAttribute
.SingleOrDefault(a => a.DataObjectId == b.DataObjectId);
if (attribute == null)
return null;
return attribute.AttributeValue;
});
They way i did is:
foreach (var objectHeaderBuffer in ohBuffer)
{
var objectHeaderAttribute = (from c in ohAttribute where c.DataObjectId == objectHeaderBuffer.DataObjectId select c).First();
objectHeaderBuffer.ReconTarget = objectHeaderAttribute.AttributeValue;
}
If your relation is 1:1 Then
foreach (ObjectHeaderBuffer Itemx in ohBuffer)
{
ObjectHeaderAttribute Itemy= (from ObjectHeaderAttribute c in ohAttribute where c.DataObjectId == Itemx.DataObjectId select c).FirstOrDefault();
if(Itemy!=null)
{
Itemx .ReconTarget = Itemy.AttributeValue;
}
}
Or
foreach (ObjectHeaderBuffer Itemx in ohBuffer)
{
ObjectHeaderAttribute Itemy= ohAttribute.Where(c=>c.DataObjectId == Itemx .DataObjectId).FirstOrDefault();
if(Itemy!=null)
{
Itemx .ReconTarget = Itemy.AttributeValue;
}
}