How to properly mapping object in fluent nhibernate - c#

Its first time when I'm using ORM.
Here is my code:
public class EwidencjonowanaRzeczMap : ClassMap<EwidencjonowanaRzecz>
{
public EwidencjonowanaRzeczMap()
{
Id(c => c.Id);
References(m => m.WagaRef);
}
}
public class WagaMap : ClassMap<Waga>
{
public WagaMap()
{
Id(m => m.Id);
Map(m => m.WagaPrzedmiotu);
HasMany(m => m.ListaRzeczy).Inverse().Cascade.All();
}
}
public class EwidencjonowanaRzecz
{
public virtual int Id { get; set; }
public virtual Waga WagaRef { get; set; }
}
public class Waga
{
public virtual int Id { get; set; }
public virtual String WagaPrzedmiotu { get; set; }
public virtual IList<EwidencjonowanaRzecz> ListaRzeczy { get; set; }
public Waga()
{
ListaRzeczy = new List<EwidencjonowanaRzecz>();
}
}
Here is my function to select records:
public IList<T> SelectAllFromTable<T>()
{
IList<T> czyDanePoprawne = null;
try
{
_sesja = NHibernateHelper.CreateSessionFactory().OpenSession();
using (_sesja.BeginTransaction())
{
czyDanePoprawne = _sesja.CreateCriteria(typeof(T)).List<T>();
}
}
catch (Exception)
{
CzyNieWystapilyBledy = true;
}
return czyDanePoprawne;
}
Tables:
ewidencjonowanarzecz [ Id - PK , IdWagi - FK (Waga.Id)]
waga [ Id - PK , WagaPrzedmiotu ]
Every type Id's - INT(3)
WagaPrzedmiotu is VARCHAR(10)
How to read to list these 2 fields?:
Id (ewidencjonowanarzecz) and WagaPrzedmiotu (waga)
In my mapping:
EwidencjaList = ModelBazy.SelectAllFromTable<EwidencjonowanaRzecz>();
is empty.
What is wrong with my mapping?

If you want to select the table record you can try this :
_sesja = NHibernateHelper.CreateSessionFactory().OpenSession();
var tabledata = _sesja.CreateQuery("from EwidencjonowanaRzecz").List<EwidencjonowanaRzecz>();
If you want to check whether if it fetches the values, try this
string value = tabledata.WagaPrzedmiotu;

Related

One-to-many update in EF Core with value object

I have 3 classes as you can see :
public class car
{
public int Id { set; get; }
public string Name { set; get; }
public List<CarTest> CarTest { get; set; }
public car(string name, int id, List<CarTest> carTests)
{
Id = id;
Name = name;
CarTest = carTests;
}
public car()
{
}
}
public class CarTest
{
public OVTest OV { get; set; }
public string CarOV { get; set; }
}
public class OVTest
{
public string Name { get; set; }
}
I want to edit my value as you can see :
static void Main(string[] args)
{
myctx myc = new myctx();
var cartests = new List<CarTest>();
var cartest = new CarTest();
var OV = new OVTest();
OV.Name = "editedName";
cartest.OV = OV;
cartest.CarOV = "2OV";
cartests.Add(cartest);
var editcar = new car("ee5155fe",2, cartests);
myc.ChangeTracker.TrackGraph(editcar, e =>e.Entry.State = EntityState.Modified);
myc.SaveChanges();
Console.ReadLine();
}
But I get this error :
System.InvalidOperationException: 'The property 'CarTestId' on entity type 'OVTest' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principal.'
This is my config:
public class CarConfig : IEntityTypeConfiguration<car>
{
public void Configure(EntityTypeBuilder<car> builder)
{
builder.OwnsMany(u => u.CarTest);
builder.OwnsMany(u => u.CarTest).OwnsOne(c=>c.OV);
}
}

Automapper 8 Ignore Attribute doesn't work on child descendant

I use AutoMapper 8.1.0 in a asp.net core project. I have an Automapper mapping that doesn't work as I expected. I reproduced the configuration so you can test it by yourself. So I have an ExpenseReport with a collection of ExpenseReportItem and this one with another collection. I have to keep the data of eTaxCollection after the mapping, but they are lost in the process.
So the question is why values of eTaxCollections are lost after calling _mapper.Map(vmodel, model) and how can I keep them?
The ignore attribute don't work. I also tried UseDestinationValue(). I lost 2 days trying to figure it out and I'm exhausted.
public void WeatherForecasts()
{
int[] excludeTaxes = new int[] { 2 };
var vmodel = new ExpenseReportCreateEditModel();
vmodel.Expenses.Add(new ExpenseReportItemModel()
{
ExcludeTaxIds = excludeTaxes,
Total = 12,
Id = 1
});
// fetch from bd
var model = new ExpenseReport();
// values will be lost after _mapper.Map...
var eTaxCollections = new HashSet<ExcludeExpenseReportItemTax>();
eTaxCollections.Add(new ExcludeExpenseReportItemTax()
{
TaxId = 1,
ExpenseReportItemId = 1
});
model.Items.Add(new ExpenseReportItem()
{
ExcludeTaxes = eTaxCollections,
ExpenseReportId = 1,
Id = 9
});
_mapper.Map(vmodel, model);
}
public class ExpenseReportCreateEditModelProfile : Profile
{
public ExpenseReportCreateEditModelProfile()
{
CreateMap<ExpenseReportCreateEditModel, ExpenseReport>()
.ForMember(d => d.Items, s => s.MapFrom(m => m.Expenses));
}
}
public class ExpenseReportItemModelProfile : Profile
{
public ExpenseReportItemModelProfile()
{
CreateMap<ExpenseReportItemModel, ExpenseReportItem>()
.ForMember(d => d.ExcludeTaxes, s => s.Ignore()); // <<<==== data are lost
}
}
public class ExpenseReportCreateEditModel
{
public int Id { get; set; }
public ICollection<ExpenseReportItemModel> Expenses { get; set; }
public ExpenseReportCreateEditModel()
{
Expenses = new HashSet<ExpenseReportItemModel>();
}
}
public class ExpenseReportItemModel
{
public int Id { get; set; }
public ICollection<int> ExcludeTaxIds { get; set; }
public decimal Total { get; set; }
public ExpenseReportItemModel()
{
ExcludeTaxIds = new HashSet<int>();
}
}
public class ExpenseReport
{
public int Id { get; set; }
public virtual ICollection<ExpenseReportItem> Items { get; set; }
public ExpenseReport()
{
Items = new HashSet<ExpenseReportItem>();
}
}
public class ExpenseReportItem
{
public int Id { get; set; }
public int ExpenseReportId { get; set; }
public virtual ICollection<ExcludeExpenseReportItemTax> ExcludeTaxes { get; set; }
public ExpenseReportItem()
{
ExcludeTaxes = new HashSet<ExcludeExpenseReportItemTax>();
}
}
public class ExcludeExpenseReportItemTax
{
public int ExpenseReportItemId { get; set; }
public virtual ExpenseReportItem ExpenseReportItem { get; set; }
public int TaxId { get; set; }
}
Thank you for any help
Edit
I execute the execution plan and perhaps this is the problem:
$typeMapDestination = ($dest ?? .New WebApplication1.Controllers.SampleDataController+ExpenseReportItem());
This is only way I can lost the values.
I have to find a solution now
Here the complete execution plan :
.If ($src == null) {
.Default(WebApplication1.Controllers.SampleDataController+ExpenseReportItem)
} .Else {
.Block() {
$typeMapDestination = ($dest ?? .New WebApplication1.Controllers.SampleDataController+ExpenseReportItem());
.Try {
.Block(System.Int32 $resolvedValue) {
.Block() {
$resolvedValue = .If (
$src == null || False
) {
.Default(System.Int32)
} .Else {
$src.Id
};
$typeMapDestination.Id = $resolvedValue
}
}
} .Catch (System.Exception $ex) {
.Block() {
.Throw .New AutoMapper.AutoMapperMappingException(
"Error mapping types.",
$ex,
.Constant<AutoMapper.TypePair>(AutoMapper.TypePair),
.Constant<AutoMapper.TypeMap>(AutoMapper.TypeMap),
.Constant<AutoMapper.PropertyMap>(AutoMapper.PropertyMap));
.Default(System.Int32)
}
};
$typeMapDestination
}
}

(Fluent) NHibernate mapping with CompositeId

I have a running database with a lot of prodcut data and I'm trying to get a (Fluent) NHibernate mapping for the structure below.
But running the code will result in an error:
Foreign key (FK1F94D86A1A0EC427:Product [ProductDet1_id])) must have same number of columns as the referenced primary key (ProductDet1 [ProductNumber, ProductionLine])
So something went wrong during mapping but can't figure out what it is. Is there somebody who can get me out of this problem? :-)
(Ofcource I let out all specific details in the tables, just to make it readable)
We hava a product table where the productnumber in combination with production line is unique. Every Product can have only one ProductDet1, ProductDet2 ... etc
MSSQL Product Table:
CREATE TABLE [dbo].[Product] (
[Id] INT NOT NULL IDENTITY,
[ProductNumber] INT NOT NULL,
[ProductionLine] INT NOT NULL,
CONSTRAINT [AK_Product_ProductNumber] UNIQUE ([ProductNumber], [ProductionLine]),
CONSTRAINT [PK_Product] PRIMARY KEY ([Id])
);
MSSQL ProductDet1 and ProductDet2 Table:
CREATE TABLE [dbo].[ProductDet1] (
[ProductNumber] INT NOT NULL,
[ProductionLine] INT NOT NULL,
[TheValue] VARCHAR (15) NULL,
CONSTRAINT [FK_ProductDet1_Product] FOREIGN KEY ([ProductNumber], [ProductionLine]) REFERENCES [Product]([ProductNumber],[ProductionLine]),
CONSTRAINT [AK_ProductDet1_ProductNumber] UNIQUE ([ProductNumber], [ProductionLine])
);
GO
CREATE INDEX [IX_ProductDet1_ProductNumber] ON [dbo].[ProductDet1] ([ProductNumber])
GO
CREATE INDEX [IX_ProductDet1_ProductionLine] ON [dbo].[ProductDet1] ([ProductionLine])
C# product class:
public class Product
{
public Product ()
{
ProductDet1 = new ProductDet1();
ProductDet2 = new ProductDet2();
}
public virtual int Id { get; set; }
public virtual int ProductionLine { get; set; }
public virtual int ProductNumber { get; set; }
public virtual string ProductName { get; set; }
public virtual ProductDet1 ProductDet1 { get; set; }
public virtual ProductDet2 ProductDet2 { get; set; }
public override bool Equals(object obj)
{
// If parameter is null return false.
if (obj == null)
{
return false;
}
// If parameter cannot be cast to Reference return false.
var product = obj as Product;
if (product == null)
{
return false;
}
// Return true if the fields match:
return this.ProductionLine == product.ProductionLine && this.ProductNumber == product.ProductNumber;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
C# product Det1 and product Det2 class:
public class ProductDet1
{
public virtual int Id { get; set; }
public virtual int ProductionLine { get; set; }
public virtual int ProductNumber { get; set; }
public virtual string TheValue { get; set; }
public virtual Product Product { get; set; }
public override bool Equals(object obj)
{
// If parameter is null return false.
if (obj == null)
{
return false;
}
// If parameter cannot be cast to Reference return false.
var product = obj as ProductCRT;
if (product == null)
{
return false;
}
// Return true if the fields match:
return this.ProductionLine == product.ProductionLine && this.ProductNumber == product.ProductNumber;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
Product Map:
public class ProductEntityMap : ClassMap<Product>
{
public ProductEntityMap()
{
Id(x => x.Id);
Map(x => x.ProductNumber);
Map(x => x.ProductionLine);
Map(x => x.ProductName);
References(x => x.ProductDet1).Cascade.All().Not.LazyLoad();
References(x => x.ProductDet2).Cascade.All().Not.LazyLoad();
}
}
Product Det 1 and Det 2 Map:
public class ProductDet1EntityMap : ClassMap<ProductDet1>
{
public ProductDet1EntityMap()
{
CompositeId().KeyProperty(x => x.ProductNumber).KeyProperty(x => x.ProductionLine);
Map(x => x.TheValue);
}
}
a slightly different class structure and mapping which hide the complexity of the det class. It essentially sets the columns used on the References()
public class Product
{
private ProductDet ProductDet1 { get; set; }
private ProductDet ProductDet2 { get; set; }
protected Product() { } // make NHibernate happy
public Product(int productionLine, int productNumber) : this(new ProductKey(productionLine, productNumber)) { }
public Product(ProductKey key)
{
Key = key;
ProductDet1 = new ProductDet { ProductKey = Key };
ProductDet2 = new ProductDet { ProductKey = Key };
}
public virtual int ID { get; protected set; }
public virtual ProductKey Key { get; protected set; }
public virtual string Det1
{
get { return ProductDet1.Value; }
set { ProductDet1.Value = value; }
}
public virtual string Det2
{
get { return ProductDet2.Value; }
set { ProductDet2.Value = value; }
}
}
public class ProductKey
{
protected ProductKey() { } // make NHibernate happy
public ProductKey(int productionLine, int productNumber)
{
ProductionLine = productionLine;
ProductNumber = productNumber;
}
public virtual int ProductionLine { get; private set; }
public virtual int ProductNumber { get; private set; }
public override bool Equals(object obj)
{
var other = obj as ProductKey;
return other != null && other.ProductionLine == this.ProductionLine && other.ProductNumber == this.ProductNumber;
}
public override int GetHashCode()
{
return (ProductionLine << 16) + ProductNumber;
}
}
public class ProductDet
{
public virtual ProductKey ProductKey { get; set; }
public virtual string Value { get; set; }
}
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Id(x => x.ID);
Component(x => x.Key, c =>
{
c.Map(k => k.ProductionLine).UniqueKey("product_key");
c.Map(k => k.ProductNumber).UniqueKey("product_key");
});
MapProductDetReference("ProductDet1");
MapProductDetReference("ProductDet2");
}
private void MapProductDetReference(string entityName)
{
References(Reveal.Member<Product, ProductDet>(entityName))
.Columns("ProductionLine", "ProductNumber")
.ReadOnly()
.EntityName(entityName)
.Cascade.All()
.Fetch.Join()
.Not.LazyLoad();
}
}
public abstract class ProductDetMap : ClassMap<ProductDet>
{
public ProductDetMap()
{
CompositeId(x => x.ProductKey)
.KeyProperty(k => k.ProductionLine)
.KeyProperty(k => k.ProductNumber);
Map(x => x.Value, "TheValue");
}
}
public class ProductDet1Map : ProductDetMap
{
public ProductDet1Map()
{
EntityName("ProductDet1");
Table("ProductDet1");
}
}
public class ProductDet2Map : ProductDetMap
{
public ProductDet2Map()
{
EntityName("ProductDet2");
Table("ProductDet2");
}
}

Records are doubled when adding referenced entities using Entity Framework

I have a problem, when i add a new entity of SessionImages, and then add another one (By uploading it), it gets doubled.
For example :
Adding an image :
Adding another one afterwards :
Here's my controller code :
[HttpPost]
public ActionResult SessionImages(FormCollection collection)
{
int SessionID = Convert.ToInt32(collection[0]);
Sessions SessionModel = sessionsRepo.GetSessionById(SessionID);
bool uploadSucceded = Utility.Utility.UploadImages(this, Request.Files, Server.MapPath(Path.Combine("~/Photos/Sessions", SessionModel.Name)));
for (int i = 0; i < Request.Files.Count; i++)
{
if (Request.Files[i].ContentLength == 0)
{
continue;
}
SessionImages ImageModel = new SessionImages
{
Name = Request.Files[i].FileName,
Path = Path.Combine("~/Photos/Sessions/", SessionModel.Name, "actual", Request.Files[i].FileName),
Session = SessionModel,
SessionID = SessionID
};
sessionImagesRepo.Add(SessionModel, ImageModel);
}
return RedirectToAction("SessionImages",SessionModel.ID);
}
Here's the sessionImagesRepo.Add method :
public SessionImages Add(Sessions SessionModel, SessionImages ImageModel)
{
try
{
context.Entry(ImageModel).State = System.Data.EntityState.Added;
SessionModel.PhotosCount = SessionModel.Images.Count;
context.Entry(SessionModel).State = System.Data.EntityState.Modified;
context.SaveChanges();
}
catch
{
return null;
}
return ImageModel;
}
Here's my entities :
namespace FP.Domain.Entities
{
public class Sessions
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime Date { get; set; }
public int PhotosCount { get; set; }
public string CoverPhoto { get; set; }
public List<SessionImages> Images { get; set; }
}
}
namespace FP.Domain.Entities
{
public class SessionImages
{
public int ImageID { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public int SessionID { get; set; }
public Sessions Session { get; set; }
}
}
Here's the configuration :
namespace FP.Domain.Configurations
{
public class SessionsConfig : EntityTypeConfiguration<Sessions>
{
public SessionsConfig()
{
ToTable("Sessions");
Property(p => p.Name).IsRequired();
Property(p => p.PhotosCount).IsRequired();
Property(p => p.CoverPhoto).IsRequired();
Property(p => p.Date).HasColumnType("date");
}
}
}
namespace FP.Domain.Configurations
{
public class SessionImagesConfig : EntityTypeConfiguration<SessionImages>
{
public SessionImagesConfig()
{
ToTable("SessionImages");
HasKey(e => e.ImageID);
Property(p => p.Path).IsRequired();
HasRequired(i => i.Session)
.WithMany(s => s.Images)
.HasForeignKey(i => i.SessionID);
}
}
}
Try changing the repository method like this:
public SessionImages Add(Sessions SessionModel, SessionImages ImageModel)
{
try
{
SessionModel.Images.Add(ImageModel);
SessionModel.PhotosCount = SessionModel.Images.Count;
context.Entry(SessionModel).State = System.Data.EntityState.Modified;
context.SaveChanges();
}
catch
{
return null;
}
return ImageModel;
}
And in the action remove the line that I've commented:
SessionImages ImageModel = new SessionImages
{
Name = Request.Files[i].FileName,
Path = Path.Combine("~/Photos/Sessions/", SessionModel.Name, "actual", Request.Files[i].FileName),
//Session = SessionModel,
SessionID = SessionID
};
I'd suggest you drop these lines, if you can workaround it (I can see the point though, in the count)...
context.Entry(ImageModel).State = System.Data.EntityState.Added;
SessionModel.PhotosCount = SessionModel.Images.Count;
context.Entry(SessionModel).State = System.Data.EntityState.Modified;
That's probably what's messing with it. I've seen similar problems more than once - and then I usually end up doing quite the opposite:
context.Entry(Parent).State = System.Data.EntityState.Unchanged;
Everything you're doing there is to get the Count working.
I can understand the optimization reasons, but you do have that count in the collection inherently. I know this is faster if you don't want to load etc.
I'm not sure what to suggest - but I'm suspecting that to be the 'culprit'.
Try w/o it, turn off and disregard count while testing. See what
happens. If that's it, then think about how to reorganize that a bit.

Displaying records in view by id

I have a table that holds Questions and i also have a table that has the Questions ID listed in an answers table. For sake of duplicate data i only use the id.
My question is how do i populate "in the view" the question based on an ID from the current table. They are already joined by MainQuestionID. I am using DBEntities code first.
Here is my viewModel for the page.
public class AuditFormEdit
{
public Models.MainQuestion MainQuestionID { get; set; }
public ICollection<MainQuestion> mainQuestion { get; set; }
public List<SubcategoryHelper> SubcategoryHelperGet { get; set; }
public class SubcategoryHelper : Models.SubCategory
{
public SubcategoryHelper(Models.SubCategory subCat)
{
this.SubCategoryID = subCat.SubCategoryID;
this.SubcategoryName = subCat.SubcategoryName;
}
}
public Models.MainAnswer MainAnswerInstance { get; set; }
public List<ScoreCardCheckBoxHelper> ScoreCardCheckBoxHelperList { get; set; }
public void InitializeScoreCheckBoxHelperList(List<Models.Score> ScoreList)
{
if (this.ScoreCardCheckBoxHelperList == null)
this.ScoreCardCheckBoxHelperList = new List<ScoreCardCheckBoxHelper>();
if (ScoreList != null
&& this.MainAnswerInstance != null)
{
this.ScoreCardCheckBoxHelperList.Clear();
ScoreCardCheckBoxHelper scoreCardCheckBoxHelper;
string scoreTypes =
string.IsNullOrEmpty(this.MainAnswerInstance.Score) ?
string.Empty : this.MainAnswerInstance.Score;
foreach (Models.Score scoreType in ScoreList)
{
scoreCardCheckBoxHelper = new ScoreCardCheckBoxHelper(scoreType);
if (scoreTypes.Contains(scoreType.ScoreName))
scoreCardCheckBoxHelper.Checked = true;
this.ScoreCardCheckBoxHelperList.Add(scoreCardCheckBoxHelper);
}
}
}
public void PopulateCheckBoxsToScores()
{
this.MainAnswerInstance.Score = string.Empty;
var scoreType = this.ScoreCardCheckBoxHelperList.Where(x => x.Checked)
.Select<ScoreCardCheckBoxHelper, string>(x => x.ScoreName)
.AsEnumerable();
this.MainAnswerInstance.Score = string.Join(", ", scoreType);
}
public class ScoreCardCheckBoxHelper : Models.Score
{
public bool Checked { get; set; }
public ScoreCardCheckBoxHelper() : base() { }
public ScoreCardCheckBoxHelper(Models.Score score)
{
this.ScoreID = score.ScoreID;
this.ScoreName = score.ScoreName;
}
}
}
AS you can see i am calling out an ICollection for the MainQuestions but i am not sure how to populate the questions. They also have to populate based on a sectionID.
Revised 2:51 9-21-2012
Or how would i relate the two if i displayed the list of questions and then when answering it would update the answers table and not the current shown table.

Categories

Resources