Castle ActiveRecord - Issue With Setter - c#

Ok, so im starting a new project and decided to use a ORM tool (as im so bored with writing it manually)
So im starting new with Castle AR,
So in my domain object ive the following
[ActiveRecord]
public class Account : ActiveRecordBase<Account>
{
private string companyName;
private Guid accountId;
[PrimaryKey(Access = PropertyAccess.FieldCamelcase)]
public Guid AccountId
{
get { return accountId; }
}
[Property(Access = PropertyAccess.FieldCamelcase)]
public string CompanyName
{
get { return companyName; }
// set { companyName= value; }
}
}
And this works and pulls out my records.
But if I uncomment the set I get the following
Obviosuly im going to need the set soon
(normally I would also remove this on CompanyName "Access=PropertyAccess.FieldCamelCase")
Any ideas what im doing wrong?

You're setting AccountId in place of accountId which creates an infinite loop. Use the fix below:
set { accountId = value; }
You're also doing the same mistake with CompanyName also so fix that too.

Related

Sequence contains no matching elements - Entity Framework

I looked all over Stack Overflow for this exception and found a lot of questions about it, but none that could solve my problem.
I am unit testing my application. All the tests were passing until I put an index on a column of one of my entities. I am using ABP, also.
It's worth noting that the app works fine when I run it, using SQL Server, but my unit tests use an in-memory database.
This works:
[Required]
public string Name
{
get { return PersonalInformation.Name; }
set { PersonalInformation.Name = value; }
}
This doesn't:
[Column(TypeName = "VARCHAR")]
[Index]
[Required]
public string Name
{
get { return PersonalInformation.Name; }
set { PersonalInformation.Name = value; }
}
The error "Sequence contains no elements" is thrown in a class I created that inherits from AbpIntegratedTestBase, in the first line of this method:
public void UsingDbContext(Action<UnitTestDbContext> action) {
using (var context = LocalIocManager.Resolve<UnitTestDbContext>())
{
action(context);
context.SaveChanges();
}
}
Can anyone help me with this? I'm trying to solve this for 3 days ^^"

Updating navigation property works but entity does not appear in Collection

I've got 2 entities, User and Material. User holds the Collection of the materials.
public class User
{
...
private ICollection<Material> materials;
public virtual ICollection<Material> Materials
{
get { return materials ?? (materials = new List<Material>()); }
set { materials = value; }
}
}
And material:
public class Material
{
...
public int? UserId { get; set; }
public virtual User User { get; set; }
...
}
When logged User selects Material, I assign that User to Material.User and save changes. Works fine, changes are saved in db. But, if I want to access materials with User.Materials, collection contains no elements. I have tried with simple project and there it works. With my complex project, it does not. What to do?
Help me please, bothering with problem for 8 hours already and still havent fix it.
EDIT: Well, that is so... lame.
My code was actually working BUT... the problem was, when view-ing user details, I retrieved User from database and created a COPY of it with a cunstrictor. I was missing something:
public User(User otherUser)
{
Id = otherUser.Id;
FirstName = otherUser.FirstName;
LastName = otherUser.LastName;
Shift = otherUser.Shift;
PassCode = otherUser.PassCode;
Type = otherUser.Type;
Materials = otherUser.Materials; // this line was missing
}
After adding this line, it works. Just Visual Studio is complaining about that (virtual member call in constructor). What to do about it?
Your problem are here:
private ICollection<Material> materials;
public virtual ICollection<Material> Materials
{
get { return materials ?? (materials = new List<Material>()); }
set { materials = value; }
}
Entity Framework navigation property cannot be property with backing field. It can be only autoproperty. So you should use this code instead:
public class User
{
...
public virtual ICollection<Material> Materials
{
get;
set;
}
}

Azure Storage Client v4.1 - a value of the non-primitive type was expected

I've recently upgraded my ASP.NET project (MVC5) to target Azure SDK 2.3 with Storage Library 4.1 and am encountering a strange error when I try to save anything to Table Storage.
Error:
An unhandled exception of type 'Microsoft.WindowsAzure.Storage.StorageException' occurred in Microsoft.WindowsAzure.Storage.dll
Additional information: A primitive value was specified; however, a value of the non-primitive type '' was expected.
My models go into table storage via repositories that use a TableServiceContext to add, update, delete, save.
I follow this pattern for my models:
[System.Data.Services.Common.DataServiceKey(new string[] { "PartitionKey", "RowKey" })]
public class PersistedAlert : Alert, ITableEntity
{
public string PartitionKey
{
get { return this.StudentId; }
set { this.StudentId = value; }
}
public string RowKey
{
get { return this.Id; }
set { this.Id = value; }
}
public DateTime Timestamp { get; set; }
public new int Type { get; set; } //hides Enum type in Alert base class
}
During the upgrade I needed to swap out all of my references to
System.Data.Services.*
for
Microsoft.Data.Services.*
...in addition to the OData libraries.
Has something changed internally that makes my pattern no longer valid?
Since there's nothing (yet) on this error on the net, and this is pretty much the only place it's discussed, I'll add a solution even though my context is different than yours. The error is exactly the same so I guess it originates from the same place.
For me, it was a inherited primary key that caused the problem. The primary key of the serialized entity has to be natural and not overriden. If Class has an ID property, DerivedClass will also have to declare the ID property as "new", or the ID property has to be moved from Class to DerivedClass.
Here's more detail: http://jerther.blogspot.ca/2014/12/aspnet-odata-v4-primitive-value-was.html
I do believe this is a bug and not a limitation as the inherited key works very well with Entity Framework and Fluent API.
I hope this helps and saves some hair pulling.
In the end I decided to upgrade all of the repository code to move away from the WCF based TableServiceContext which is deprecated and instead make calls via CloudTable. I can only assume something internally changed to one of the above mentioned libraries that resulted in the problem I was seeing.
In addition to changing the repository code, I also needed to update my entities to inherit from the Azure ITableEntity (I had my own flavour previously), like this:
public class PersistedAlert : Alert, Microsoft.WindowsAzure.Storage.Table.ITableEntity
{
public string PartitionKey
{
get { return this.StudentId; }
set { this.StudentId = value; }
}
public string RowKey
{
get { return this.Id; }
set { this.Id = value; }
}
public DateTimeOffset Timestamp { get; set; }
public string ETag { get; set; }
public void ReadEntity(IDictionary<string, Microsoft.WindowsAzure.Storage.Table.EntityProperty> properties, Microsoft.WindowsAzure.Storage.OperationContext operationContext)
{
Microsoft.WindowsAzure.Storage.Table.TableEntity.ReadUserObject(this, properties, operationContext);
}
public IDictionary<string, Microsoft.WindowsAzure.Storage.Table.EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext)
{
return Microsoft.WindowsAzure.Storage.Table.TableEntity.WriteUserObject(this, operationContext);
}
}

C# Class for UserData accross the whole Forms

i am trying to implement a windows desktop applicaiton c# VS 2010. To Application has a login form to ask the user to type his username and password. and the applicaiton checks them in the DB and returns the userdata (id, permissions, email, telephone) which are saved in DB.
Programmatically, when the login data is valid and correct, i create an instant from a class classed "clsUser" which provides the fields for the users.
and then fill the class with the data as normal and then open the main form and child forms after that.
My question is , how to access the class of user over the whole applicaiton (for example to check if he has the permission to access the Form or not). I tried different approaches like Calling string permission = FormLogin.clsUser.permission(); but its not fine .
thanks for your help or any suggestions !!
int id;
string fname;
string lname;
string uUsername;
public clsUser()
{ }
public int UserID
{
get { return id; }
set { id = value; }
}
public string FirstName
{
get { return fname; }
set { fname = value; }
}
public string LastName
{
get { return lname; }
set { lname = value; }
}
public string Username
{
get { return uUsername; }
set { uUsername = value; }
}
public override string ToString()
{
return base.ToString();
}
You can access to this object over the whole application by making it static:
public static class clsUser() { }
Now you can access its properties with:
string userPermission = clsUser.Permission;
You can set a property by:
clsUser.Permission = "Administrator";
You dont have to create a new Instance of a static class. You can access to it over the whole application by calling it with its class name (in your case clsUser) and the property name you want to access like above written.
Hope this is useful ;)
you can create a Global class using the singleton pattern. Inside that you can hold your actual User as a Property and access it via
var user = Global.Current.User;
var permission = Global.Current.User.Permission;
I found a topic for the singleton pattern here
Thread Safe C# Singleton Pattern

One property not saving when using web service

I have this object:
public class Announcement
{
public int Id { get; set; }
public DateTime DateSent { get; set; }
private IList<string> _recipients;
public IList<string> Recipients
{
get { return _recipients; }
set { _recipients = value; }
}
public string RecipientsString
{
get { return String.Join("\n", _recipients); }
set { _recipients = value.Split('\n').ToList(); }
}
}
I can populate this object with the DateSent and RecipientString (a string of email addresses separated by \n) and save it to the database with no problems.
Now I want to move this to a web service so we can use it across multiple apps.
I created the exact same object in the webservice, and testing locally (on the service) everything works as expected.
But if I populate the object on the client and pass it to the service to be saved, the RecipientString is always empty (not null). The DateSent is fine.
I'm guessing the data is getting lost in serialization, but I don't know why, or how to solve this. I thought also, it could have something to do with the # in the email address, but I've ruled that out. Any suggestions?
This happens because de WSDL that is generated to describe your service can't describe the function that is used in your get and set functions. I suggest you keep RecipientsString as a common property, and create a private method GetRecipients on your class that processes the RecipientsString value and returns the list you need.
Use RecipientsString without backing field.

Categories