I am using Glass Mapper for Umbraco. While trying to model something I have a class like:
[UmbracoType(AutoMap = true)]
public class ImageWithLink : BaseUmbracoItem
{
public virtual Image Image { get; set; }
public virtual ?? Link { get; set; }
public virtual string Copy { get; set; }
}
There does not seem to be a 'Link' data type like there is in the Sitecore implementation. I saw This post ( http://bluetubeinc.com/blog/2014/6/glass-mapper-and-umbraco-7) and they use the 'RelatedLink' data type, but that does not exsist ( i checked in the glass repository).
Do I have to model it my self?
Edit: This is the Related Links property type.
Assuming that Umbraco hasn't changed in a while and that you mean a link as in a html anchor...
I may be wrong on this one, but from memory, Umbraco has / had no concept of a link when it comes to the return from the api. It is returned as a node id (or list of) and you must use an extension method to return the url from.
Like you, as far as I can see in the Glass code base, it doesn't have an explicit implementation for a 'link' type like we have in Sitecore.
My guess is that you would have to either roll your own using a custom class and the Delegate feature OR use an integer mapping and call the umbraco api method.
I would also guess is that RelatedLink in that example is mapping to another class which would use the UmbracoPropertyTypeMapper in a similar way to what we do in Sitecore between types, not a 'Link' as in anchor.
We are due to look at umbraco again during the V4 process I believe so talk to Mike about adding it as a feature.
I Found a (rather horrible) solution. Umbraco returns Json, so i had to de-serialize it. You could turn this into a mapper.
[UmbracoType(AutoMap = true)]
public class BaseUmbracoItem : IUmbracoItem
{
public virtual string Links { get; set; }
public List<UmbracoLink> TypedLink
{
get
{
return JsonConvert.DeserializeObject<List<UmbracoLink>>(Links);
}
}
}
public class UmbracoLink
{
public string link { get; set; }
public string type { get; set; }
public string title { get; set; }
public bool newWindow { get; set; }
public bool isInternal { get; set; }
}
Here is a mapper version:
public class UmbracoLinkMapper : AbstractDataMapper
{
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoLinkMapper"/> class.
/// </summary>
public UmbracoLinkMapper()
{
ReadOnly = true;
}
/// <summary>
/// Maps data from the .Net property value to the CMS value
/// </summary>
/// <param name="mappingContext">The mapping context.</param>
/// <returns>The value to write</returns>
/// <exception cref="System.NotSupportedException"></exception>
public override void MapToCms(AbstractDataMappingContext mappingContext)
{
throw new NotSupportedException();
}
/// <summary>
/// Maps data from the CMS value to the .Net property value
/// </summary>
/// <param name="mappingContext">The mapping context.</param>
/// <returns>System.Object.</returns>
public override object MapToProperty(AbstractDataMappingContext mappingContext)
{
var scContext = mappingContext as UmbracoDataMappingContext;
var scConfig = Configuration as UmbracoLinkConfiguration;
var properties = scContext.Content.Properties.Where(x => x.Alias == Configuration.PropertyInfo.Name.ToLower()).ToList();
if (properties.Any())
{
var property = properties.First().Value as string;
return JsonConvert.DeserializeObject<List<UmbracoLink>>(property).First();
}
return null;
}
/// <summary>
/// Indicates that the data mapper will mapper to and from the property
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="context">The context.</param>
/// <returns><c>true</c> if this instance can handle the specified configuration; otherwise, <c>false</c>.</returns>
public override bool CanHandle(AbstractPropertyConfiguration configuration, Context context)
{
return configuration is UmbracoLinkConfiguration;
}
}
public class UmbracoLinkConfiguration : AbstractPropertyConfiguration
{
public bool IsLazy { get; set; }
public bool InferType { get; set; }
}
public class UmbracoLinkAttribute : AbstractPropertyAttribute
{
public bool IsLazy { get; set; }
public bool InferType { get; set; }
public override AbstractPropertyConfiguration Configure(PropertyInfo propertyInfo)
{
var config = new UmbracoLinkConfiguration { IsLazy = IsLazy, InferType = InferType };
Configure(propertyInfo, config);
return config;
}
}
Related
I am creating a Ntier solution which includes Domain Objects, DataAccess Layer,Service layer and the web API layer . I am using AutoMapper to map Dtos and domain objects in the service layer. I would like to know how to write the logic for performing CRUD operations in the service layer. I have written some mapping. Is this the right way of mapping or is there a better way to do it and also please do correct me where I have written the get, save, update, delete operation. I basically need help in implementing my Service layer.
I am getting the following error when I debug the code:
Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.
I get the error at the following line of code in the GetPatient method:
yield return Mapper.Map<PatientDto>(patient);
Domain layer
public class Patient : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public char Gender { get; set; }
public string Phone { get; set; }
}
DataAccess Layer
public class GenericRepository<TEntity> where TEntity : class
{
#region Private member variables...
internal AppointmentBookingContext Context;
internal DbSet<TEntity> DbSet;
#endregion
#region Public Constructor...
/// <summary>
/// Public Constructor,initializes privately declared local variables.
/// </summary>
/// <param name="context"></param>
public GenericRepository(AppointmentBookingContext context)
{
this.Context = context;
this.DbSet = context.Set<TEntity>();
}
#endregion
#region Public member methods...
/// <summary>
/// generic Get method for Entities
/// </summary>
/// <returns></returns>
public virtual IEnumerable<TEntity> Get()
{
IQueryable<TEntity> query = DbSet;
return query.ToList();
}
/// <summary>
/// Generic get method on the basis of id for Entities.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public virtual TEntity GetByID(object id)
{
return DbSet.Find(id);
}
/// <summary>
/// generic Insert method for the entities
/// </summary>
/// <param name="entity"></param>
public virtual void Insert(TEntity entity)
{
DbSet.Add(entity);
}
/// <summary>
/// Generic Delete method for the entities
/// </summary>
/// <param name="id"></param>
public virtual void Delete(object id)
{
TEntity entityToDelete = DbSet.Find(id);
Delete(entityToDelete);
}
/// <summary>
/// Generic Delete method for the entities
/// </summary>
/// <param name="entityToDelete"></param>
public virtual void Delete(TEntity entityToDelete)
{
if (Context.Entry(entityToDelete).State == EntityState.Detached)
{
DbSet.Attach(entityToDelete);
}
DbSet.Remove(entityToDelete);
}
/// <summary>
/// Generic update method for the entities
/// </summary>
/// <param name="entityToUpdate"></param>
public virtual void Update(TEntity entityToUpdate)
{
DbSet.Attach(entityToUpdate);
Context.Entry(entityToUpdate).State = EntityState.Modified;
}
/// <summary>
/// generic method to get many record on the basis of a condition.
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public virtual IEnumerable<TEntity> GetMany(Func<TEntity, bool> where)
{
return DbSet.Where(where).ToList();
}
/// <summary>
/// generic method to get many record on the basis of a condition but query able.
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public virtual IQueryable<TEntity> GetManyQueryable(Func<TEntity, bool> where)
{
return DbSet.Where(where).AsQueryable();
}
/// <summary>
/// generic get method , fetches data for the entities on the basis of condition.
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public TEntity Get(Func<TEntity, Boolean> where)
{
return DbSet.Where(where).FirstOrDefault<TEntity>();
}
/// <summary>
/// generic delete method , deletes data for the entities on the basis of condition.
/// </summary>
/// <param name="where"></param>
/// <returns></returns>
public void Delete(Func<TEntity, Boolean> where)
{
IQueryable<TEntity> objects = DbSet.Where<TEntity>(where).AsQueryable();
foreach (TEntity obj in objects)
DbSet.Remove(obj);
}
/// <summary>
/// generic method to fetch all the records from db
/// </summary>
/// <returns></returns>
public virtual IEnumerable<TEntity> GetAll()
{
return DbSet.ToList();
}
/// <summary>
/// Inclue multiple
/// </summary>
/// <param name="predicate"></param>
/// <param name="include"></param>
/// <returns></returns>
public IQueryable<TEntity> GetWithInclude(
System.Linq.Expressions.Expression<Func<TEntity,
bool>> predicate, params string[] include)
{
IQueryable<TEntity> query = this.DbSet;
query = include.Aggregate(query, (current, inc) => current.Include(inc));
return query.Where(predicate);
}
/// <summary>
/// Generic method to check if entity exists
/// </summary>
/// <param name="primaryKey"></param>
/// <returns></returns>
public bool Exists(object primaryKey)
{
return DbSet.Find(primaryKey) != null;
}
/// <summary>
/// Gets a single record by the specified criteria (usually the unique identifier)
/// </summary>
/// <param name="predicate">Criteria to match on</param>
/// <returns>A single record that matches the specified criteria</returns>
public TEntity GetSingle(Func<TEntity, bool> predicate)
{
return DbSet.Single<TEntity>(predicate);
}
/// <summary>
/// The first record matching the specified criteria
/// </summary>
/// <param name="predicate">Criteria to match on</param>
/// <returns>A single record containing the first record matching the specified criteria</returns>
public TEntity GetFirst(Func<TEntity, bool> predicate)
{
return DbSet.First<TEntity>(predicate);
}
#endregion
}
Service layer
PatientDto.cs
public class PatientDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public char Gender { get; set; }
public string Phone { get; set; }
}
AutoMapperConfiguration.cs
public class AutoMapperConfiguration
{
public static void Configure()
{
Assembly[] assemblies = BuildManager.GetReferencedAssemblies().OfType<Assembly>().ToArray();
Mapper.Initialize(cfg =>
cfg.AddProfiles(AllClasses.FromAssemblies(assemblies)
.Where(
a =>
a.FullName.EndsWith("Mapping")))); }
}
DomainToDtoMapping.cs
public class DomainToDtoMapping : Profile
{
public DomainToDtoMapping()
{
CreateMap<BaseEntity, BaseDto>().ReverseMap();
CreateMap<Patient, PatientDto>().ReverseMap();
}
}
IPatientService
public interface IPatientService
{
IEnumerable<PatientDto> GetPatient();
PatientDto GetPatientById(int id);
int CreatePatient(PatientDto customer);
bool UpdatePatient(PatientDto patient);
bool DeletePatient(int patient);
}
PatientService
public class PatientService : IPatientService
{
private readonly IUnitOfWork _unitOfWork;
public int CreatePatient(PatientDto patientDto)
{
using (var scope = new TransactionScope())
{
var patient = _unitOfWork.PatientRepository.GetByID(patientDto.Id);
_unitOfWork.PatientRepository.Insert(patient);
_unitOfWork.Save();
scope.Complete();
return patient.Id;
}
}
public bool DeletePatient(int id)
{
var success = false;
if (id > 0)
{
using (var scope = new TransactionScope())
{
var patient = _unitOfWork.PatientRepository.GetByID(id);
if (patient != null)
{
_unitOfWork.PatientRepository.Delete(patient);
_unitOfWork.Save();
scope.Complete();
success = true;
}
}
}
return success;
}
public IEnumerable<PatientDto> GetPatient()
{
var patient = _unitOfWork.PatientRepository.GetAll();
if (patient != null)
{
yield return Mapper.Map<PatientDto>(patient);
}
yield return null;
}
public PatientDto GetPatientById(int id)
{
var patient = _unitOfWork.PatientRepository.GetByID(id);
if (patient != null)
{
return Mapper.Map<PatientDto>(patient);
}
return null;
}
public bool UpdatePatient(PatientDto patientDto)
{
var success = false;
if (patientDto != null)
{
using (var scope = new TransactionScope())
{
var patient = _unitOfWork.PatientRepository.GetByID(patientDto.Id);
if (patient != null)
{
_unitOfWork.PatientRepository.Update(patient);
_unitOfWork.Save();
scope.Complete();
success = true;
}
}
}
return success;
}
I am using Swagger for WebApi 5.5.3 nuget package for API documentation. In swagger UI it is showing required option for optional parameter.
I tried XML comment option in Visual studio. Below is the API method that i want to document:
/// <summary>
/// Gets the history.
/// </summary>
/// <param name="currentPageIndex">Index of the current page.</param>
/// <param name="pageSize">Size of the page.</param>
/// <param name="lastSyncDate">The last synchronize date.</param>
/// <returns></returns>
[HttpGet]
[Route("GetHistory/{currentPageIndex}/{pageSize}")]
public IHttpActionResult GetHistory(int currentPageIndex, int pageSize, DateTime? lastSyncDate)
{
var response = _myRepo.GetData();
if (response == null)
return BadRequest(Messages.InvalidPageIndex);
return Ok(response);
}
It is showing lastSyncDate as query parameter but it is required while I have marked it as nullable parameter.
I have also tried making currentPageIndex as nullable in xml as well as route but still all the properties as showing as required. Please help.
Solution for this problem given below
Create Model class
using System;
using System.ComponentModel.DataAnnotations;
public class SearchHistory
{
[Required]
public int CurrentPageIndex { get; set; }
[Required]
public int PageSize { get; set; }
public DateTime? LastSyncDate { get; set; }
}
Change your input parameter with newly create model
[HttpGet]
public IHttpActionResult GetHistory(SearchHistory modle)
{
var response = _myRepo.GetData();
if (response == null)
return BadRequest(Messages.InvalidPageIndex);
return Ok(response);
}
Hope this will solve your issue.
I have problem with this Conference table. The error is :
Invalid column name 'Conference_ConferenceID'
namespace MeetingBoard.Model
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Script.Serialization;
using MeetingBoard.Model.Helpers;
using System.ComponentModel.DataAnnotations.Schema;
/// <summary>
/// A model of the Conference entity. Contains functionality to serialize the entity to JSON as well.
/// </summary>
public class Conference
{
[Key]
public int ConferenceID { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int CreatorID { get; set; }
public string Location { get; set; }
public DateTime SubmissionDate { get; set; }
[ForeignKey("CreatorID")]
public virtual User Creator { get; set; }
public int[] RelatedProjectsIDs { get; set; }
public virtual ICollection<ProjectTag> RelatedProjectTags { get; set; }
public DateTime CreatedOn
{
get { return (this.dateCreated == default(DateTime)) ? DateTime.UtcNow : this.dateCreated; }
set { this.dateCreated = value; }
}
private DateTime dateCreated = default(DateTime);
public virtual ICollection<Group> RelatedGroups { get; set; }
public Conference()
{
RelatedGroups = new List<Group>();
}
/// <summary>
/// Generates an object that can be serialized by the JSON serializer of MVC
/// </summary>
/// <param name="happening">An Conference.</param>
/// <returns></returns>
public static Object ToJsonObject(Conference conference)
{
int[] project_ids = conference.RelatedProjectTags.Select<ProjectTag, int>(pt => pt.ProjectID).ToArray();
return new Conference_JSON
{
id = conference.ConferenceID,
title = conference.Title,
Content = conference.Content,
created_timestamp_UTC = Util.DateTimeToMilliTimeStamp(conference.CreatedOn),
SubmissionDate = conference.SubmissionDate,
Location = conference.Location,
creator_avatar = conference.Creator.Avatar,
creator_fullname = conference.Creator.Name,
creator_id = conference.Creator.UserID,
project_ids = project_ids,
};
}
/// <summary>
/// Instantiates a new Conference object based on the json data.
/// </summary>
/// <param name="json_data">The json data needs to have the structure as specified in the private Conference_JSON object.</param>
/// <returns>A new Conference object. The related projects are referenced using an integer array containing project ids.</returns>
public static Conference FromJson(String json_data)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
Conference_JSON conference_object = serializer.Deserialize<Conference_JSON>(json_data);
return FromJsonObject(conference_object);
}
/// <summary>
/// Instantiates a new Conference object based on the private Conference_JSON object.
/// </summary>
/// <param name="json_data">The object needs to be an instance of the private Conference_JSON object.</param>
/// <returns>A new Conference object. The related projects are referenced using an integer array containing project ids.</returns>
public static Conference FromJsonObject(Object conference_object)
{
Conference_JSON conference_json = (Conference_JSON)conference_object;
Conference conference = new Conference
{
ConferenceID = conference_json.id,
Title = conference_json.title,
Content = conference_json.Content,
RelatedProjectsIDs = conference_json.project_ids,
Location = conference_json.Location,
SubmissionDate = conference_json.SubmissionDate,
};
return conference;
}
/// <summary>
/// Defines the structure of the json objects that ar communicated to and from the Frontend.
/// </summary>
private class Conference_JSON
{
/// <summary>
/// The Conference identifier.
/// </summary>
public int id;
public string title;
public string Content;
/// <summary>
/// An numeric representation of the time, in milliseconds from Unix Epoch, UTC timezone.
/// </summary>
public double created_timestamp_UTC;
public string creator_fullname;
public int creator_id;
public string creator_avatar;
/// <summary>
/// Related projects.
/// </summary>
public int[] project_ids;
public string Location;
public DateTime SubmissionDate;
}
}
}
I get this error when there is a mismatch between the code and the DB, in the sense that the code expects to find columns in the DB but they don't exist there. This happens when the DB isn't updated to match the changes in the code. I'd suggest looking at the database that is being hit when you get that error, maybe it's not looking where you expect.
As I'm working on an ASP.NET MVC project I've saw a weird behavior of EF which delays me (to be exact, I'm still stuck on this problem at least month... and only now I've realized that my DDD architecture code is not broken and it's specific an EF-related code bug that I have).
My site has posts. Each post has a set of attributes (PostAttributeValue) and each attribute value has a related PostAttributeDefinition which contains data about it - such as Title, Validation Rules, Raw Value (binary serialized), data type etc.
This is my Post model:
public class Post
{
#region Settings
/// <summary>
/// Maximum linked images
/// </summary>
public static int MaximumLinkedImages
{
get { return Properties.Settings.Default.MaximumPostsLinkedImages; }
}
/// <summary>
/// Maximum linked image size in MB
/// </summary>
public static int MaximumLinkedImageSize
{
get { return Properties.Settings.Default.MaximumPostLinkedImageSize; }
}
/// <summary>
/// Delay hours between posts bumping
/// </summary>
public static int BumpPostDelayHours
{
get { return Properties.Settings.Default.BumpPostDelayHours; }
}
#endregion
#region ctor
public Post()
{
this.Attributes = new List<PostAttributeValue>();
this.LinkedImages = new List<string>();
}
#endregion
/// <summary>
/// The parent category that this post was posted into
/// </summary>
[Required]
public virtual Category ParentCategory { get; set; }
/// <summary>
/// The post unique identifier
/// </summary>
[Key]
public Guid PostIdentifier { get; set; }
/// <summary>
/// The post title (e.g. "Great vila!")
/// </summary>
[Required]
public string Title { get; set; }
/// <summary>
/// The post title url alias (e.g. "great-vila")
/// </summary>
public string TitleUrlAlias { get; set; }
/// <summary>
/// Post extra notes and information written by the author
/// </summary>
[Required]
public string Description { get; set; }
/// <summary>
/// The post item city
/// </summary>
[Required]
public virtual City City { get; set; }
/// <summary>
/// The post item location
/// </summary>
public string Location { get; set; }
/// <summary>
/// Is the post was published and marketed by brokerage (Tivuuch)
/// </summary>
[Required]
public bool Brokerage { get; set; }
/// <summary>
/// Post custom attributes
/// </summary>
public virtual ICollection<PostAttributeValue> Attributes { get; set; }
/// <summary>
/// The post assigned price
/// </summary>
[Required]
public int RequestedPrice { get; set; }
/// <summary>
/// List of images linked with the post (includes only the name of the picture, a.k.a "foo.png", "bar.jpg" etc.)
/// </summary>
public virtual ICollection<string> LinkedImages { get; set; }
public string LinkedImagesSerialized
{
get
{
if (this.LinkedImages == null)
{
this.LinkedImages = new List<string>();
}
return string.Join(",", this.LinkedImages);
}
set
{
if (this.LinkedImages == null)
{
this.LinkedImages = new List<string>();
}
if (string.IsNullOrEmpty(value))
{
return;
}
this.LinkedImages = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
}
/// <summary>
/// Cached generated cached url using IShorterUrlService
/// </summary>
public string GeneratedShorterUrl { get; set; }
/// <summary>
/// Is this post marked as hot
/// </summary>
public bool IsHotPost { get; set; }
/// <summary>
/// The post publish status
/// </summary>
public PostPublishStatus PublishStatus { get; set; }
/// <summary>
/// The post author
/// </summary>
public virtual Account Author { get; set; }
/// <summary>
/// The author IP address (collected to determine different IPs)
/// </summary>
public string AuthorIPAddress { get; set; }
/// <summary>
/// The creation date of the post
/// </summary>
public DateTime CreationDate { get; set; }
/// <summary>
/// The last post modification date
/// </summary>
public DateTime LastUpdatedDate { get; set; }
/// <summary>
/// The date that the post was bumped at, used to sort the posts in category.
/// </summary>
public DateTime LastBumpDate { get; set; }
}
This is PostAttributeValue
public class PostAttributeValue
{
///
/// The attribute value id
///
[Key]
public int AttributeValueId { get; set; }
/// <summary>
/// The value owner post
/// </summary>
public virtual Post OwnerPost { get; set; }
/// <summary>
/// The value attribute definition id
/// </summary>
//public int RelatedAttributeDefinitionId { get; set; }
/// <summary>
/// The value attribute definition
/// </summary>
public virtual PostAttributeDefinition Definition { get; set; }
/// <summary>
/// The stored raw value
/// </summary>
public byte[] RawValue { get; set; }
}
and this is PostAttributeDefinition
public class PostAttributeDefinition
{
///
/// The filter name
///
[Key]
public int DefinitionId { get; set; }
/// <summary>
/// The owner category
/// </summary>
[Required]
public virtual Category OwnerCategory { get; set; }
/// <summary>
/// The filter title
/// </summary>
[Required]
public string Title { get; set; }
/// <summary>
/// Metadata enum that provides extra data about the data type
/// </summary>
public PostAttributeTypeMetadata TypeMetadata { get; set; }
/// <summary>
/// Bitwise metadata that provides data about the object in display mode
/// </summary>
public PostAttributeDisplayModeMetadata DisplayModeMetadata { get; set; }
public PostAttributeEditorType EditorType { get; set; }
/// <summary>
/// The attribute raw default value
/// </summary>
[Required]
public byte[] RawDataValue { get; set; }
/// <summary>
/// The attribute raw associated validation attributes
/// </summary>
public byte[] RawValidationRules { get; set; }
/// <summary>
/// Is this field required
/// </summary>
public bool IsRequired { get; set; }
}
My problem is that when I'm trying to add a new post I'm getting a relationship error (A relationship from the AssociationSet is in the 'Deleted' state)
which is
A relationship from the 'PostAttributeValue_Definition' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'PostAttributeValue_Definition_Source' must also in the 'Deleted' state.
Now, I've saw that the problem is that when I'm assigning to PostAttributeValue a definition, automaticlly it becomes Deleted - even if I'm assigning a definition that I'm fetching from the DB right now.
I've tested the above code:
var db = ServiceLocator.SharedInstance.GetInstance<MyDbContext>();
List<PostAttributeValue> v = new List<PostAttributeValue>();
var entity = db.PostAttributesDefinitions.Where(d => d.DefinitionId==1).Include(d => d.OwnerCategory).First();
v.Add(new PostAttributeValue() { Definition = entity });
Post post = new Post()
{
Title = "foo",
Description = "bar",
City = new City { },
Brokerage = false,
Location = "",
RequestedPrice = 500,
ParentCategory = new Category() { },
AuthorIPAddress = "",
Attributes = v
};
db.Posts.Add(post);
var deletedValuesStore = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)db)
.ObjectContext.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Deleted);
And saw that deletedValuesStore contains 1 item - the definition. When I'm commenting this line there's no items in the store.
Do you got any ideas how it can be solved?
Thanks for your time!
Ok so I figured it out.
Just in case somebody is interest, I've configured my DbContext mistakenly with Required().WithOptional() relationship instead of one to many - Required().WithMany().
Because of that, when I've assigned existing attribute definition, which was already assigned to a value, to a new value - it automatically marked it as deleted.
I just recently switched from EF5 to NHibernate due to a few features that I want in my ORM but didn't find in EF. So, I'm new to NHibernate. I'm working in ASP.Net MVC.
I'm using Automapper to map FNH objects to my view models, but I'm having issues translating previously how I did things in EF to FNH. For example, I have a self referencing table that is a menu system.
Here is the model:
public partial class Menu {
private int _Id;
private string _Title;
private string _Link;
private int _SortOrder;
private System.Nullable<int> _ParentMenuId;
private Iesi.Collections.ISet _ChildMenus;
private Menu _ParentMenu;
#region Extensibility Method Definitions
partial void OnCreated();
#endregion
public Menu()
{
this._ChildMenus = new Iesi.Collections.HashedSet();
OnCreated();
}
/// <summary>
/// There are no comments for Id in the schema.
/// </summary>
public virtual int Id
{
get
{
return this._Id;
}
set
{
this._Id = value;
}
}
/// <summary>
/// There are no comments for Title in the schema.
/// </summary>
public virtual string Title
{
get
{
return this._Title;
}
set
{
this._Title = value;
}
}
/// <summary>
/// There are no comments for Link in the schema.
/// </summary>
public virtual string Link
{
get
{
return this._Link;
}
set
{
this._Link = value;
}
}
/// <summary>
/// There are no comments for SortOrder in the schema.
/// </summary>
public virtual int SortOrder
{
get
{
return this._SortOrder;
}
set
{
this._SortOrder = value;
}
}
/// <summary>
/// There are no comments for ParentMenuId in the schema.
/// </summary>
public virtual System.Nullable<int> ParentMenuId
{
get
{
return this._ParentMenuId;
}
set
{
this._ParentMenuId = value;
}
}
/// <summary>
/// There are no comments for ChildMenus in the schema.
/// </summary>
public virtual Iesi.Collections.ISet ChildMenus
{
get
{
return this._ChildMenus;
}
set
{
this._ChildMenus = value;
}
}
/// <summary>
/// There are no comments for ParentMenu in the schema.
/// </summary>
public virtual Menu ParentMenu
{
get
{
return this._ParentMenu;
}
set
{
this._ParentMenu = value;
}
}
}
Here is the Mapping:
public class MenuMap : ClassMap<Menu>
{
public MenuMap()
{
Schema(#"dbo");
Table(#"Menus");
LazyLoad();
Id(x => x.Id)
.Column("Id")
.CustomType("Int32")
.Access.Property()
.CustomSqlType("int")
.Not.Nullable()
.Precision(10)
.GeneratedBy.Identity();
Map(x => x.Title)
.Column("Title")
.CustomType("String")
.Access.Property()
.Generated.Never()
.CustomSqlType("varchar");
Map(x => x.Link)
.Column("Link")
.CustomType("String")
.Access.Property()
.Generated.Never()
.CustomSqlType("varchar")
.Not.Nullable()
.Length(50);
Map(x => x.SortOrder)
.Column("SortOrder")
.CustomType("Int32")
.Access.Property()
.Generated.Never()
.Not.Nullable()
.UniqueKey("KEY1");
Map(x => x.ParentMenuId)
.Column("ParentMenuId")
.CustomType("Int32")
.Access.Property()
.Generated.Never()
.UniqueKey("KEY1");
HasMany<Menu>(x => x.ChildMenus)
.Access.Property()
.AsSet()
.Cascade.None()
.LazyLoad()
.Inverse()
.Not.Generic()
.KeyColumns.Add("ParentMenuId", mapping => mapping.Name("ParentMenuId")
.SqlType("int")
.Nullable());
References(x => x.ParentMenu)
.Class<Menu>()
.Access.Property()
.Cascade.None()
.LazyLoad()
.Columns("ParentMenuId");
}
}
Here is my View Model or DTO:
public class MainMenuItemViewModel
{
public Int32 Id { get; set; }
public string Title { get; set; }
public string Link { get; set; }
public Int32 SortOrder { get; set; }
public Int32? ParentMenuId { get; set; }
public IList<MainMenuItemViewModel> ChildMenus { get; set; }
}
When I try to map the domain object to the view model, using this:
Mapper.CreateMap<Menu, MainMenuItemViewModel>();
I get the following error on I check if the configuration is valid on run:
The following property on WinStream.WebUI.Models.MainMenuItemViewModel cannot be mapped: ChildMenus
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type WinStream.WebUI.Models.MainMenuItemViewModel.
Context:
Mapping to property ChildMenus from System.Object to WinStream.WebUI.Models.MainMenuItemViewModel
Mapping to property ChildMenus from Iesi.Collections.ISet to System.Collections.Generic.IList`1[[WinStream.WebUI.Models.MainMenuItemViewModel, WinStream.WebUI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Mapping from type WinStream.Services.Entities.Menu to WinStream.WebUI.Models.MainMenuItemViewModel
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.
I thought it might be related to converting ISet to IList, so I put in an ISet in my view model, but still had the issue.
Thank you for your help - I realize this could be a complete newbie question, but I couldn't find much help through Google. I've been struggling with this for several days now.
Thanks!
EDIT:
I have gotten past the error above, but now when I'm querying the database, the ChildMenus collection for the root object includes a null object for every child object in the database, including the associated child objects, instead of just the actual related child objects.
For example:
Root Menu
ChildMenus collection is supposed to have 3 child objects, but it has 8 (5 null and 3 populated)
List item
ChildMenus collection is supposed to have 1 child objects, but it has 8 (7 null and 1 populated)
List item
ChildMenus collection is supposed to have 0 child objects, and it has no child objects.
This is the code:
IList<Menu> menus = session.Query<Menu>().Where(x => x.ParentMenuId== null).ToList()
Any ideas on this, or do I need to put it into another question? Thank you!
NHibernate doesn't need a lot of the workarounds from EF. You basicly have a menu with ordered childmenues having a parent reference.
public class Menu
{
public int Id { get; protected set; }
public string Title { get; set; }
public string Link { get; set; }
public IList<Menu> ChildMenus { get; protected set; }
public Menu ParentMenu { get; set; }
public Menu()
{
ChildMenus = new List<Menu>();
}
}
public class MenuMap : ClassMap<Menu>
{
public MenuMap()
{
Table(#"Menus");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Title).Length(100);
Map(x => x.Link).Length(50);
HasMany<Menu>(x => x.ChildMenus)
.AsList("SortOrder")
.Inverse()
.KeyColumn("ParentMenuId");
References(x => x.ParentMenu).Column("ParentMenuId");
}
}
Notes:
Schema should be defined by convention or as default schema/catalog in the Configuration object
remove all unnessesary declarations from the mapping because it often introduces portability issues (eg. customsqltypes), complicates the code and prevents conventions
customsqltype() renders length() useless
Sortorder is not really needed because the list already defines the order
parentId is duplicate for Parent.Id and can be implemented if needed ParentId { get { return ParentMenu == null ? null : (int?)ParentMenu.Id } }, no need to map or store it in field
if the parentreference is not needed remove it and .Inverse() from the collection mapping