I've got the following classes:
public class Client {
public virtual Guid ClientID { get; set; }
public virtual string ClientName { get; set; }
public virtual IList<ClientMonthlyRevenue> Revenue { get; set; }
...
public virtual void SetMonthlyRevenue(int year, int month, double revenue)
{
// Make sure it's not null... this might happen depending on how the client is created
if (Revenue == null)
Revenue = new List<ClientMonthlyRevenue>();
// Check for existance - we don't want any duplicates
ClientMonthlyRevenue clientMonthlyRevenue = Revenue.Where(x => x.Year == year && x.Month == month).FirstOrDefault();
if (clientMonthlyRevenue == null)
{
// If it doesn't exist, create a new one and add to the list
clientMonthlyRevenue = new ClientMonthlyRevenue(this, year, month, revenue);
this.Revenue.Add(clientMonthlyRevenue); // This is the line throwing the error
}
else
{
// If it exists, just update it
clientMonthlyRevenue.Revenue = revenue;
}
}
}
public class ClientMonthlyRevenue {
public virtual Client ParentClient { get; set; }
public virtual int Year { get; set; }
public virtual int Month { get; set; }
public virtual double Revenue { get; set; }
...
}
And these two mappings:
public class ClientMap : ClassMap<Client>
{
Id(x => x.ClientID).GeneratedBy.Assigned();
Map(x => x.ClientName);
HasMany<ClientMonthlyRevenue>(x => x.Revenue)
.Table("ClientMonthlyRevenue")
.KeyColumn("ClientID")
.Cascade.All()
.Fetch.Join();
}
public class ClientMonthlyRevenueMap : ClassMap<ClientMonthlyRevenue>
{
CompositeId()
.KeyReference(x => x.Client, "ClientID")
.KeyProperty(x => x.Year)
.KeyProperty(x => x.Month);
Map(x => x.Revenue);
}
When I get a Client from the database:
Client client = Session.Get<Client>(clientID);
all the data is there, which is great. But when I try to add a new ClientMonthlyRevenue child:
client.Revenue.Add(new ClientMonthlyRevenue(this.ClientID, year, month, revenue));
I get the error:
Collection was of a fixed size.
Am I missing or misunderstanding something here? And what do I need to modify to be able to add items to this persisted list?
I would change the Client object to have the following:
public class Client
{
public Client()
{
Revenue = new List<ClientMonthlyRevenue>();
}
public virtual Guid ClientID { get; set; }
public virtual string ClientName { get; set; }
public virtual IList<ClientMonthlyRevenue> Revenue { get; set; }
public virtual void AddRevenue(ClientMonthlyRevenue revenue)
{
revenue.ParentClient = this;
Revenue.Add(revenue);
}
}
Then you can call like this:
public void TestMapping()
{
session.BeginTransaction();
var client = new Client{ClientID = Guid.NewGuid()};
session.SaveOrUpdate(client);
client = session.Get<Client>(client.ClientID);
client.AddRevenue(new ClientMonthlyRevenue(2001,07,1200));
session.Transaction.Commit();
}
The error you are receiving sounds like it could be created higher up in the stack. I was able to recreate your scenario. See full source: https://gist.github.com/1098337
have you tried to mark your collection as Inverse? I dont know if it could help.
HasMany<ClientMonthlyRevenue>(x => x.Revenue)
.Table("ClientMonthlyRevenue")
.KeyColumn("ClientID")
.Cascade.All()
.Fetch.Join()
.Inverse();
Related
My mode class:
public class AccountCustomer
{
public bool IsMain { get; set; }
public int AccountId { get; set; }
public Account Account { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
public class Account
{
public int Id { get; set; }
public strig No{ get; set; }
..other fields
public ICollection<AccountCustomer> AccCustomer { get; set; } = new List<AccountCustomer>();
}
public class Customer
{
public int Id { get; set; }
public strig Name{ get; set; }
..other fields
public ICollection<AccountCustomer> AccCustomer { get; set; } = new List<AccountCustomer>();
}
I found soluton here and i have implemented same later i found that it is for without extra column
Many to many ef core updaate
Please let meknow how to do update... Am using latest ef 5 preview
My code :
_context.Set<AccountCustomer>().UpdateLinks(ac => ac.AccountId, account.Id,
ac => ac.CustomerId, account.AccCustomer .Select(ac => ac.CustomerId));
Codes of ManyToMany Update Extensions
Remove the unselected item and add new item to list.
public static class Extensions
{
public static void TryUpdateManyToMany<T, TKey>(this DbContext db, IEnumerable<T> currentItems, IEnumerable<T> newItems, Func<T, TKey> getKey) where T : class
{
db.Set<T>().RemoveRange(currentItems.ExceptThat(newItems, getKey));
db.Set<T>().AddRange(newItems.ExceptThat(currentItems, getKey));
}
private static IEnumerable<T> ExceptThat<T, TKey>(this IEnumerable<T> items, IEnumerable<T> other, Func<T, TKey> getKeyFunc)
{
return items
.GroupJoin(other, getKeyFunc, getKeyFunc, (item, tempItems) => new { item, tempItems })
.SelectMany(t => t.tempItems.DefaultIfEmpty(), (t, temp) => new { t, temp })
.Where(t => ReferenceEquals(null, t.temp) || t.temp.Equals(default(T)))
.Select(t => t.t.item);
}
}
Codes of Update ViewModel
The update view pass it to action via request.
public class AccountCustomerVM
{
public bool IsMain { get; set; }
public Account Account { get; set; }
public List<int> Customers { get; set; }
}
Codes of Update Action
[HttpPut]
public IActionResult Update(AccountCustomerVM accountCustomerVM)
{
var model = _context.Accounts.Include(x => x.AccCustomer).FirstOrDefault(x => x.Id == accountCustomerVM.Account.Id);
_context.TryUpdateManyToMany(model.AccCustomer, accountCustomerVM.Customers
.Select(x => new AccountCustomer
{
IsMain = accountCustomerVM.IsMain,
CustomerId = x,
AccountId = accountCustomerVM.Account.Id
}), x => x.CustomerId);
_context.SaveChanges();
return Ok();
}
I have an Account table that stores Master Accounts and Sub Accounts. Master Accounts are basically the same as Sub Accounts except that they can have an associated Company. Account is an abstract class and both MasterAccount and SubAccount derive from it.
A MasterAccount is any account entry with a null ParentAccountId. If an Account record has a ParentAccountId then it is a SubAccount and the ParentAccountId references the AccountId field for the MasterAccount.
I am trying get FluentNhibernate mappings for them.
The classes look like the following
public class Account : EntityBase
{
public Account() { }
public virtual string AccountNumber { get; set; }
public virtual string AccountName { get; set; }
public virtual string ContactRole { get; set; }
public virtual bool EmailBillDataFile { get; set; }
public virtual bool EmailBill { get; set; }
public virtual bool PostBill { get; set; }
public virtual BillingMethod BillingMethod { get; set; }
public virtual BillingAddressType BillingAddressType { get; set; }
public virtual Contact Contact { get; set; }
public virtual bool IsInvoiceRoot { get; set; }
public virtual string Password { get; set; }
public virtual bool HasRequestedInvoicing { get; set; }
public virtual bool IsInternational { get; set; }
public virtual decimal AmountPaid { get; set; }
public virtual decimal PreviousBill { get; set; }
public virtual void MakePayment(decimal amount)
{
MakePayment(amount, null);
}
public virtual void MakePayment(decimal amount, string invoiceNumber)
{
AmountPaid += amount;
if (string.IsNullOrEmpty(invoiceNumber))
LogActivity(string.Format("Made payment of {0:c}", amount));
else {
LogActivity(string.Format("Made payment of {0:c} on Invoice '{1}'", amount, invoiceNumber));
}
}
public virtual Invoice CreateInvoice()
{
Invoice invoice;
invoice = IsInternational ? new NoGstInvoice() : new Invoice();
// Can update invoice properties that rely on account data here.
return invoice;
}
#region Business Rules
public override IEnumerable<RuleViolation> GetRuleViolations()
{
if (string.IsNullOrEmpty(AccountName))
yield return new RuleViolation("Account Name required", "AccountName");
if (string.IsNullOrEmpty(AccountNumber))
yield return new RuleViolation("Acocunt Number required", "AccountNumber");
if (string.IsNullOrEmpty(Password))
yield return new RuleViolation("Password required", "Password");
yield break;
}
#endregion
}
public class MasterAccount : Account
{
private Company _company;
private IList<SubAccount> _subAccounts;
public MasterAccount() : this(null) { }
public MasterAccount(Company company)
{
_company = company;
_subAccounts = new List<SubAccount>();
}
public virtual Company Company
{
get { return _company; }
}
public virtual IEnumerable<SubAccount> SubAccounts
{
get { return _subAccounts; }
}
public virtual SubAccount CreateSubAccount(string accountNumber, string accountName)
{
var subAccount = new SubAccount(this)
{
AccountName = accountName,
AccountNumber = accountNumber,
Contact = this.Contact,
ContactRole = this.ContactRole,
PreviousBill = 0,
AmountPaid = 0,
BillingAddressType = this.BillingAddressType,
BillingMethod = this.BillingMethod,
IsInternational = this.IsInternational,
IsInvoiceRoot = false,
EmailBill = this.EmailBill,
EmailBillDataFile = this.EmailBillDataFile,
Password = this.Password,
PostBill = this.PostBill
};
return subAccount;
}
}
public class SubAccount : Account
{
private MasterAccount _masterAccount;
public SubAccount() { }
public SubAccount(MasterAccount master)
{
_masterAccount = master;
}
public virtual MasterAccount MasterAccount
{
get { return _masterAccount; }
}
}
The mappings I have are:
public class AccountMap : ClassMap<Account>
{
public AccountMap()
{
Table("Account");
Id(x => x.Id).Column("AccountId").GeneratedBy.Identity();
Map(x => x.AccountName).Length(50).Not.Nullable();
Map(x => x.AccountNumber).Length(10).Not.Nullable();
Map(x => x.ContactRole).Length(50);
Map(x => x.BillingMethod).Not.Nullable();
Map(x => x.EmailBill).Not.Nullable();
Map(x => x.PostBill).Not.Nullable();
Map(x => x.EmailBillDataFile).Not.Nullable();
Map(x => x.BillingAddressType).Not.Nullable();
Map(x => x.IsInvoiceRoot).Not.Nullable();
Map(x => x.HasRequestedInvoicing).Not.Nullable();
Map(x => x.IsInternational).Not.Nullable();
Map(x => x.PreviousBill).Not.Nullable();
Map(x => x.AmountPaid).Not.Nullable();
Map(x => x.Password).Length(20).Not.Nullable();
References(x => x.Contact).Column("ContactId").Not.Nullable();
DiscriminateSubClassesOnColumn("ParentAccountId");
}
}
public class MasterAccountMap : SubclassMap<MasterAccount>
{
public MasterAccountMap()
{
References(x => x.Company).Column("CompanyId");
HasMany(x => x.SubAccounts).KeyColumn("ParentAccountId").Inverse().Cascade.All();
}
}
public class SubAccountMap : SubclassMap<SubAccount>
{
public SubAccountMap()
{
References(x => x.MasterAccount).Column("ParentAccountId").Not.Nullable();
}
}
However, when I execute the following test:
[Test]
public void Can_add_subAccount_to_database()
{
var master = Session.Get<MasterAccount>(1);
var subAccount = master.CreateSubAccount("TST123", "Test Account");
Session.Save(subAccount);
Session.Flush();
Session.Clear();
var fromDb = Session.Get<SubAccount>(subAccount.Id);
Assert.AreNotSame(subAccount, fromDb);
}
I get an exception on the Session.Save(subAccount); line.
System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
I do not get the exception if I comment out the References mapping in SubAccountMap.
Any help on correctly mapping this relationship is appreciated.
Seems I needed to use the Formula method off DiscriminateSubClassesOnColumn
DiscriminateSubClassesOnColumn("").Formula("case when parentaccountid is null then '0' else '1' end");
and then use the following in each of the subclasses
DiscriminatorValue("0"); // In MasterAccountMap
DiscriminatorValue("1"); // in SubAccountMap
see http://wiki.fluentnhibernate.org/Fluent_mapping
and should I save the DTOs directly to the database?
public class Product
{
public virtual int ProductId { get; set; }
public virtual string ProductCode { get; set; }
public virtual string ProductName { get; set; }
public virtual Category Category { get; set; }
}
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Id(x => x.ProductId);
Map(x => x.ProductCode);
Map(x => x.ProductName);
References(x => x.Category).Column("CategoryId");
}
}
public class ProductDto
{
public virtual int ProductId { get; set; }
public virtual string ProductCode { get; set; }
public virtual string ProductName { get; set; }
public virtual int CategoryId { get; set; }
}
public class ProductDtoMap : ClassMap<ProductDto>
{
public ProductDtoMap()
{
Table("Product");
Id(x => x.ProductId);
Map(x => x.ProductCode);
Map(x => x.ProductName);
Map(x => x.CategoryId);
}
}
Here's how I create, open and save record:
public ActionResult Input()
{
return View(new ProductDto());
}
public ActionResult Edit(int id)
{
using(var s = SessionFactoryBuilder.GetSessionFactory().OpenSession())
{
return View("Input", s.Get<ProductDto>(id));
}
}
// Save
[HttpPost]
public ActionResult Input(ProductDto p)
{
if (ModelState.IsValid)
{
using (var s = SessionFactoryBuilder.GetSessionFactory().OpenSession())
{
s.Merge(p);
s.Flush();
}
return RedirectToAction("Index");
}
else
{
return View(p);
}
}
Suffice to say, I wanted convenience, I want to persist the DTOs directly to database, and retrieve them back directly too.
Now what need do I have for Product class if I just use ProductDto exclusively for CRUD? I'll just use Product class for reporting only :-)
Is populating DTOs directly from ORM and saving them back directly to ORM a sound practice?
Yes if you can do it, it's the best way. Don't feel guilty about it :)
i'm trying to remove an item from a one to many list and have it persist in the database. Here are the entities i have defined:
public class SpecialOffer
{
public virtual int SpecialOfferID { get; set; }
public virtual string Title { get; set; }
public virtual IList<SpecialOfferType> Types { get; private set; }
public SpecialOffer()
{
Types = new List<SpecialOfferType>();
}
}
public class SpecialOfferType
{
public virtual SpecialOffer SpecialOffer { get; set; }
public virtual Type Type { get; set; }
public virtual int MinDaysRemaining { get; set; }
#region composite id requirements
public override bool Equals(object obj)
{
if (obj == null || !(obj is SpecialOfferType))
return false;
var t = (SpecialOfferType)obj;
return SpecialOffer.SpecialOfferID == t.SpecialOffer.SpecialOfferID && Type.TypeID == t.Type.TypeID;
}
public override int GetHashCode()
{
return (SpecialOffer.SpecialOfferID + "|" + Type.TypeID).GetHashCode();
}
#endregion
}
public class Type
{
public virtual int TypeID { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
}
With the following fluent mappings:
public class SpecialOfferMap : ClassMap<SpecialOffer>
{
public SpecialOfferMap()
{
Table("SpecialOffers");
Id(x => x.SpecialOfferID);
Map(x => x.Title);
HasMany(x => x.Types)
.KeyColumn("SpecialOfferID")
.Inverse()
.Cascade.All();
}
}
public class SpecialOfferTypeMap : ClassMap<SpecialOfferType>
{
public SpecialOfferTypeMap()
{
Table("SpecialOfferTypes");
CompositeId()
.KeyReference(x => x.SpecialOffer, "SpecialOfferID")
.KeyReference(x => x.Type, "TypeID");
Map(x => x.MinDaysRemaining);
}
}
public class TypeMap : ClassMap<Type>
{
public TypeMap()
{
Table("Types");
Id(x => x.TypeID);
Map(x => x.Title);
Map(x => x.Price);
}
}
The problem i have is that if i remove an item from the SpecialOffer.Types collection it successfully removes it from the list but when i try to save the session the change is not persisted in the database. I'm assuming this is something to do with the composite id on the join table since i have been able to do this successfully in the past with a standard id.
I'd appreciate it if someone could show me what i'm doing wrong. Thanks
I think you have to 1) Change the cascade setting on SpecialOffer.Types to Cascade.AllDeleteOrphan() and 2) set SpecialOfferType.SpecialOffer = null when you remove it from the collection. Since the collection is the inverse side of the relationship, the many-to-one reference to SpecialOffer on SpecialOfferType has to be set to null to make it an orphan, then Cascade.AllDeleteOrphan will cause it to be deleted.
I can't figure out how to solve the following problem.
What i need it a relationship from one base class to another, so that every derived class has a relationship with the same table, called 'Item' in my example.
Since this is just an example it doesn't reflect my program. In the real program the relationship with class Item is in a different namespace. Therefore it can't be in the derived class.
The error:
A key is registered for the derived type 'WebApplication1.Client'. Keys must be registered for the root type 'WebApplication1.Base'.
namespace WebApplication1
{
public class Item
{
public int ItemID { get; set; }
}
public class Base
{
public int ID { get; set; }
public int ItemID { get; set; }
public Item Item { get; set; }
}
public class Client : Base
{
public string Name { get; set; }
private List<Project> _projects = null;
public List<Project> Projects
{
get
{
if (_projects == null)
_projects = new List<Project>();
return _projects;
}
}
}
public class Project : Base
{
public string Name { get; set; }
public int ClientId { get; set; }
public Client Client { get; set; }
}
public class Main
{
public static void Test()
{
ContextBuilder<ObjectContext> ContextBuilder = new ContextBuilder<ObjectContext>();
var itemConfig = new EntityConfiguration<Item>();
itemConfig.HasKey(p => p.ItemID);
itemConfig.Property(p => p.ItemID).IsIdentity();
ContextBuilder.Configurations.Add(itemConfig);
var clientConfig = new EntityConfiguration<Client>();
clientConfig.HasKey(p => p.ID);
clientConfig.Property(p => p.ID).IsIdentity();
clientConfig.Property(p => p.Name);
clientConfig.Relationship(p => p.Item).HasConstraint((p, c) => p.ItemID == c.ItemID);
ContextBuilder.Configurations.Add(clientConfig);
var projectConfig = new EntityConfiguration<Project>();
projectConfig.HasKey(p => p.ID);
projectConfig.Property(p => p.ID).IsIdentity();
projectConfig.Property(p => p.Name);
projectConfig.Relationship(p => p.Item).HasConstraint((p, c) => p.ItemID == c.ItemID);
projectConfig.Relationship(p => p.Client).FromProperty(p => p.Projects).HasConstraint((p, c) => p.ClientId == c.ID);
ObjectContext objCtx = ContextBuilder.Create(new SqlConnection(#"Data Source=(local);Initial Catalog=testa;Integrated Security=SSPI;"));
if (!objCtx.DatabaseExists())
objCtx.CreateDatabase();
}
}
}
Look at how do deal with inheritance mapping here: Link
For basic non-relational proberties and how-to reuse them: https://danielwertheim.wordpress.com/2009/11/29/entity-framework-4-how-to-reuse-mappings-and-add-a-concurrency-token/