[CSHARP-630] Saving to Different Collection Created: 12/Nov/12  Updated: 20/Mar/14  Resolved: 14/Nov/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: william tell Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7



 Description   

I have the following model:

public class Account: Entity
{
public string Email

{ get; set; }
public string Name { get; set; }

public string Designation

{ get; set; }
public string Telephone { get; set; }

public string Mobile

{ get; set; }
public string CompanyId { get; set; }

}

public class Store : Entity
{
[Required]
[DataMember]
public string Name

{ get; set; }
[Required]
[DataMember]
public string Address1 { get; set; }

[DataMember]
public string Address2

{ get; set; }
[Required]
[DataMember]
public string City { get; set; }

[DataMember]
[DisplayName("State/Province")]
public string State

{ get; set; }
[Required]
[DataMember]
public int Zip { get; set; }


[DataMember]
public string CompanyId

{ get; set; }

}

And the following repository:

public class AccountRepository : MongoRepository<Account> , IAccountRepository {}

public class StoreRepository : MongoRepository<Store>, IStoreRepository{}

When i try to add an item to the account with the code below, an instance is also added to the store. What code be the cause of the store addition? I have debug my code to ensure that there are no call to StoreRepository add. Thanks in advance.

AccountRepository repo = new AccountRepository();
repo.Add(new Got2FindMe.Infrastracture.Model.Account

{ Id = id, Email = model.Email }

);



 Comments   
Comment by william tell [ 14/Nov/12 ]

I see. This is a great info. Thank you very much.

William

Comment by Craig Wilson [ 13/Nov/12 ]

If you look in the Util.cs class at http://mongorepository.codeplex.com/SourceControl/changeset/view/20204#236959, you'll find the problem. There is a method at the bottom called GetCollectionNameFromType where it derives the collection name from your entity by it's type. If it has a base type that is NOT Entity, it uses that. Hence, when you insert a middle man (EntityBase), then all your types end up getting persisted in the same collection.

You can use a [CollectionName] attribute on your classes (Store, Account, etc...) to set them to something you know.

Hope that helps...

Comment by william tell [ 13/Nov/12 ]

Hi Craig,

That was a quick response. Thank you very much. I found the source of issue but I still don’t know what is happening. I am using Official MongoDb c# driver from NuGet (Version 1.6). The model I posted in your website was out of date, please see belew for the updated model. The error I encountered is that when I derived the Entity for my own entity base then inherit it for my model's, querying collections seem's to be having problem. When I removed the inheritance from my EntityBase and let the model inherit derictely from DreamSongs.MongoRepository's Entity, the error was gone and everything seems to be working good.

//Entity is from the DreamSongs.MongoRepository assembly
public class EntityBase: Entity
{
public Status Status

{ get; set; }

}
//erroneus
public class Account: EntityBase

{ //codes here }

//erroneus
public class Store : EntityBase
{ //codes here }
//working as expected
//Entity was directly inherited
public class Account: Entity
{ //codes here }

//working as expected
//Entity was directly inherited
public class Store : Entity

{ //codes here }

For now, I will stick to inheriting directly to the Entity. It will be good help if you can let me know what is happening.

Again Thank you so much,
William

Comment by Craig Wilson [ 12/Nov/12 ]

Hi William,
These types of questions are better posted at our google group here (https://groups.google.com/forum/?fromgroups=#!forum/mongodb-user) or on stackoverflow.com.

I this particular example, could you please let us know what a MongoRepository<T> is? This is not a part of our driver and so some research will need to be conducted in order to help you with your problem.

Thanks,
Craig

Generated at Wed Feb 07 21:37:23 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.