I have written an interface for a class as follows:
public interface IGenericMultipleRepository
{
Lazy<IGenericRepository<Address>> addresses { get; set; }
Lazy<IGenericRepository<Asset>> assets { get; set; }
}
I am implementing this interface as follows:
public class GenericMultipleRepository : IGenericMultipleRepository
Here are the values that I have implemented:
public Lazy<IGenericRepository<Address>> addresses;
public Lazy<IGenericRepository<Asset>> assets;
Here are the errors that I am getting:
'CanFindLocation.Repositories.GenericMultipleRepository' does not
implement interface member
'CanFindLocation.Interfaces.IGenericMultipleRepository.assets'
'CanFindLocation.Repositories.GenericMultipleRepository' does not
implement interface member
'CanFindLocation.Interfaces.IGenericMultipleRepository.addresses'
How can I implement the interface IGenericMultipleRepository without any errors.
Thanks in advance.
The only reason I can see is since you are implementing an interface that you forgot to put
Lazy<IGenericRepository<Address>> addresses { get; set; }
Lazy<IGenericRepository<Asset>> assets { get; set; }
inside your GenericMultipleRepository class definition
Also, your
public Lazy<IGenericRepository<Address>> addresses; only creates a variable, which doesn`t match with what the compiler is expecting.
Your compiler is expecting a Property not variables.
Related
There might be something wrong in my model that I cannot figure out since I get the following error when trying to make a migration:
"An error occurred while calling method 'BuildWebHost' on class Program. Continuing without the application service provider. Error: Field 'k__BackingField' of entity type 'MapeoArticuloProveedor' is readonly and so cannot be set.
Unable to create an object of type 'NSideoContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time."
Entities:
[Table("MapeosArticuloProveedor", Schema = "public")]
public class MapeoArticuloProveedor
{
public string Codigo { get; set; }
public int? IdLadoDeMontaje { get; set; }
[ForeignKey("IdLadoDeMontaje")]
public virtual LadoDeMontajeMapeoArticulo LadoDeMontaje { get; }
}
[Table("LadosDeMontajeMapeosArticulos", Schema = "public")]
public class LadoDeMontajeMapeoArticulo
{
public string Codigo { get; set; }
public string Valor { get; set; }
}
What could it be?
#WanneBDeveloper basically you're exposing the property since you made it public. A more conservative approach would be to set it as follows:
public LadoDeMontajeMapeoArticulo LadoDeMontaje { get; private set; }
note the private keyword
Then the propery can only be set from within the class and not outside of it. Therefore you'll know which class is mutating it's state.
this is your issue:
public virtual LadoDeMontajeMapeoArticulo LadoDeMontaje { get; }
Basically the error is saying you can't set the "LadoDeMontaje" once it is retrieved.
Simply change it to:
public virtual LadoDeMontajeMapeoArticulo LadoDeMontaje { get; set; }
I have a requirement to call a 3rd party rest api using service stack and this is working fine.
But one of the rest api's requires a property called "public"
Is there an attribute I can specify to give it another name in the class but use the public name when it calls the service?
so I have this definition in the class
public string public { get; set; }
The error I get is
Member modifier 'public' must precede the member type and name
Thanks
OK I found what I needed.
I tried the Alias attribute from ServiceStack.DataAnotations.Alias but this did nothing and I am not sure what it is for?
I then found that adding a reference to System.Runtime.Serialization was needed and also adorning the class with
[System.Runtime.Serialization.DataContract]
Now each public property needs the following attribute or it will not pass the parameter to the rest server. IN the case of the property called Public it specifies the name in the DataMember attribute constructor.
[System.Runtime.Serialization.DataMember]
Below is an example
[System.Runtime.Serialization.DataContract]
public class RequestVoiceBaseSearch : VoiceBaseBaseClass, IReturn<ResponseVoiceBaseSearch>
{
[System.Runtime.Serialization.DataMember]
public string action { get; set; }
[System.Runtime.Serialization.DataMember]
public string terms { get; set; }
[System.Runtime.Serialization.DataMember]
public string from { get; set; }
[System.Runtime.Serialization.DataMember]
public string to { get; set; }
[System.Runtime.Serialization.DataMember(Name = "Public")]
public bool _public { get; set; }
[System.Runtime.Serialization.DataMember]
public string rank { get; set; }
public RequestVoiceBaseSearch()
: base()
{
this.action = "Search";
this.terms = "";
}
}
Chris
How can I use a virtual property in an interface in Entity Framework?:
public interface ICommentable
{
int CommentableId { get; set; }
virtual ICollection<Comment> Comments { get; set; }
}
That throws an error for the virtual keyword, but if I do it this way:
public interface ICommentable
{
int CommentableId { get; set; }
ICollection<Comment> Comments { get; set; }
}
Then this (below) throws an error ("does not implement ...")
public class Something : ICommentable
{
int CommentableId { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
...more properties....
}
It throws an error because CommentableId is not public. It has nothing to do with placing virtual on Comments in the class which is valid.
Using virtual in interface is not valid because it doesn't make sense. When you define interface, the implementing class must implement all its members. Virtual keyword means that derived class can override existing implementation but the interface has no implementation.
I'm using MVC3 VS2010 with EF4.1, I have created my DB using SQL Server and I import it to the MVC3 Web Application.
I have a challenge here, when I come to Update Model from Database I do lost all my models files modifications, for example if I'm using attributes in some models for validation or so all that is overwritten with the new model properties.
Is there anyway to Update Model from Database without losing models' information?
OR
where should I define validation on my models instead of using the models' files directly?
Update: As this is still relatively popular, I have created a blog post on this.
http://jnye.co/Posts/19/adding-validation-to-models-created-by-entity-framework-database-first-c
If you want to validate your models, and not use viewModels, use partial classes to define validation attributes. For example:
Say you have a model like
public class User {
public string Name { get; set; }
}
If you wanted to put a string length validator on it you would need to create a partial class and utilise the MetadataTypeAttribute (this lives in System.ComponentModel.DataAnnotations)
The following classes should be defined in their own separate file, NOT put in the same file as your auto generated models.
[MetadataTypeAttribute(typeof(UserMetadata))]
public partial class User {
}
You then define your validation in the UserMetadata class as follows
public class UserMetadata{
[StringLength(50)]
public string Name {get; set;}
}
EDIT
I just found this article which explains the solution in a little more detail
http://themonitoringguy.com/tips-tricks/validating-microsoft-entity-framework-objects-c-mvc/
No, the files will be regenerated every time.
All the classes are defined as partial so you can easily add DataAnnotations using the MetadataTypeAttribute.
Let's say you have a User class defined as follow:
public partial class User {
public string Name {get;set;}
}
Create a IUser interface
public interface IUser {
[Required]
[DisplayName("User name")]
string Name {get;set;}
}
And then extend the User class to specify that IUser will be used as metadata.
[MetadataType(typeof(IUser))]
public partial class User {} //Empty class body
The first rule of any designer is: It it generates any code you can't modify it because it will be completely deleted next time you update anything in the designer.
All generated classes are partial so you can create your own partial part and put your custom logic there. You obviously can't add attributes to properties defined in auto generated part. In case of data annotations it is possible either through buddy classes or by custom T4 template which will contain your own logic to decide which data annotation should be added during code generation. Both scenarios are mostly considered as a bad practice because you should have separate view model per view with validation needed exactly for that view.
Check the namespace of the MainClass is same as Partial, and have the same Attributes. That is my solution.
example:
Metadata: Create this everywhere u want
public class FormMetadata
{
public int Id { get; set; }
public string Description { get; set; }
public Nullable<bool> IsEnable { get; set; }
public Nullable<System.DateTime> CreationDate { get; set; }
public int CompanieId { get; set; }
public string RegularExpression { get; set; }
public virtual ICollection<Field> Fields { get; set; }
[JsonIgnore]
public virtual Company Company { get; set; }
}
MainClass
namespace Transactions.Model
{
public partial class Form
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Form()
{
this.Fields = new HashSet<Field>();
}
public int Id { get; set; }
public string Description { get; set; }
public Nullable<bool> IsEnable { get; set; }
public Nullable<System.DateTime> CreationDate { get; set; }
public int CompanieId { get; set; }
public string RegularExpression { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Field> Fields { get; set; }
public virtual Company Company { get; set; }
}
}
Partial To Use the MetadataType
namespace Transactions.Model
{
[MetadataTypeAttribute(typeof(FormMetadata))]
public partial class Form
{
}
}
If you have problems to Create a Class Partial in the same NameSpace? Don't worry:
Create a Folder
Create the Class Partial in this folder
Change Namespace at the same of MainClass
I typed this simplified example without the benefit of an IDE so forgive any syntax errors. When I try to automap this I get a FluentConfigurationException when I attempt to compile the mappings -
"Association references unmapped class
IEmployee."
I imagine if I were to resolve this I'd get a similar error when it encounters the reference to IEmployer as well. I'm not opposed to creating a ClassMap manually but I prefer AutoMapper doing it instead.
public interface IEmployer
{
int Id{ get; set; }
IList<IEmployee> Employees { get; set; }
}
public class Employer: IEmployer
{
public int Id{ get; set; }
public IList<IEmployer> Employees { get; set; }
public Employer()
{
Employees = new List<IEmployee>();
}
}
public interface IEmployee
{
int Id { get; set; }
IEmployer Employer { get; set; }
}
public class Employee: IEmployee
{
public int Id { get; set;}
public IEmployer Employer { get; set;}
public Employee(IEmployer employer)
{
Employer = employer;
}
}
I've tried using .IncludeBase<IEmployee>() but to no avail. It acts like I never called IncludeBase at all.
Is the only solution to either not use interfaces in my domain entities or fall back on a manually defined ClassMap?
Either option creates a significant problem with the way my application is designed. I ignored persistence until I had finished implementing all the features, a mistake I won't be repeating again :-(
It's not a restriction imposed by Fluent or its AutoMapper, but by NHibernate itself.
I therefore don't think you'd get there with the manual class map. You'll have to lose the interfaces in the property and list definitions. You can keep the interfaces, but mapped properties and collections must use the concrete types of which NHibernate knows.
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.Id);
Map<Address>(x => x.Address); // Person.Address is of type IAddress implemented by Address
}
}