How to programmatically load a specific file for NCache client configuration? - ncache

Not wanting to leave a client.ncconf lying aside my exe, I wish to be able to specify the path to the client.ncconf file programatically. How may I? I am using NCache 4.4SP1 Open Source.
The methods I am using are mainly Web.Caching.NCache.InitializeCache and Cache.Get.

It picks the config from %NCHOME% InstallDir/config.
Just add the following in your AppSettings
<add key="InstallDir" value="C:\temp"/>
Also, all the client configurations can be specified programmatically using the CacheInitParams. You can
namespace Alachisoft.NCache.Web.Caching
{
public class CacheInitParams : ICloneable
{
public CacheInitParams();
public string BindIP { get; set; }
public ClientCacheSyncMode ClientCacheSyncMode { get; set; }
public int ClientRequestTimeOut { get; set; }
public int CommandRetries { get; set; }
public int CommandRetryInterval { get; set; }
public int ConnectionRetries { get; set; }
public int ConnectionTimeout { get; set; }
public string DefaultReadThruProvider { get; set; }
public string DefaultWriteThruProvider { get; set; }
public bool LoadBalance { get; set; }
public CacheMode Mode { get; set; }
[Obsolete("This property is deprecated. Please use the 'ServerList' property instead.", false)]
public int Port { get; set; }
public SecurityParams PrimaryUserCredentials { get; set; }
public int RetryConnectionDelay { get; set; }
public int RetryInterval { get; set; }
public SecurityParams SecondaryUserCredentials { get; set; }
[Obsolete("This property is deprecated. Please use the 'ServerList' property instead.", false)]
public string Server { get; set; }
public CacheServerInfo[] ServerList { get; set; }
public object Clone();
}
}

Related

Entity Framework many to many relation error

I'm trying to create a many-to-many relationship between my two tables, but when I run Update-Database command I get this error:
Introducing FOREIGN KEY constraint 'FK_dbo.ExamQuestions_dbo.Questions_Question_Id' on table 'ExamQuestions' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
My first entity is :
public class Question
{
public Question()
{
this.Exams = new HashSet<Exam>();
}
[Key]
public int Id { get; set; }
[Required(ErrorMessage="Question is Required")]
[Display(Name="Question")]
[AllowHtml]
public string QuestionText { get; set; }
// public bool IsMultiSelect { get; set; }
public string Hint { get; set; }
public string HelpLink { get; set; }
public int Marks { get; set; }
public byte[] ImageData { get; set; }
[StringLength(50)]
public string MimeType { get; set; }
public byte[] Audio { get; set; }
public int QuestionTypeId { get; set; }
public int TopicId { get; set; }
public int DifficulityLevelId { get; set; }
public int SubjectId { get; set; }
public DifficultyLevel QuestionDifficulity { get; set; }
public Topic Topic { get; set; }
public virtual ICollection<Option> Options { get; set; }
public ICollection<Exam> Exams { get; set; }
}
And the second entity is:
public class Exam
{
public Exam()
{
this.Questions = new HashSet<Question>();
}
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Duration { get; set; }
[Required]
public int TotalQuestion { get; set; }
[Required]
public int TotalMarks { get; set; }
public bool SectionWiseTime { get; set; }
public bool QuestionWiseTime { get; set; }
public bool AllQustionRequired { get; set; }
public bool AllowBackForward { get; set; }
public bool SuffleSubjectWise { get; set; }
public bool SuffleOptionWise { get; set; }
public bool GroupSubjectWise { get; set; }
[Required]
public int ExamTypeId { get; set; }
[Required]
public int ExamInstructionId { get; set; }
[Required]
public int DifficultyLevelId { get; set; }
public virtual ExamType ExamType { get; set; }
public virtual ExamInstruction ExamInstruction { get; set; }
public virtual DifficultyLevel DifficultyLevel { get; set; }
public virtual ICollection<Question> Questions { get; set; }
//public virtual ICollection<ExamSchedule> ExamSchedules { get; set; }
}
Can someone tell me where I'm going wrong?
By default, EF has cascading deletes set. This error is warning you that this can cause cascading deletes with many to many relationships. And is probably not what you intend to have happen on a delete/update.
You can remove the OneToManyCascadeDeleteConvention in the OnModelCreating method, or on the fluent mapping for each entity.
Details are provided in this SO Answer

How can I set up two navigation properties of the same type in Entity Framework without use Fluent API

i'm trying create DB using codefirst. i want to create two ForeingKey from same table. But when i set up two navigation properties of the same type, get error like :
The foreign key name 'FollowedUser' was not found on the dependent type Models.UserUserWatchListItem'. The Name value should be a comma separated list of foreign key property names.
public class UserUserWatchListItem
{
public int Id { get; set; }
[Key,ForeignKey("FollowedUser")]
public virtual User FollowedUser { get; set; }
public int FollowedUserId { get; set; }
[Key,ForeignKey("FolloweeUser")]
public int FolloweeUserId { get; set; }
public virtual User FolloweeUser { get; set; }
}
Use this :
public class UserUserWatchListItem
{
public int Id { get; set; }
public int FollowedUserId { get; set; }
public int FolloweeUserId { get; set; }
[ForeignKey("FollowedUser")]
[InverseProperty("FollowedUsers")]
public virtual User FollowedUser { get; set; }
[ForeignKey("FolloweeUser")]
[InverseProperty("FolloweeUsers")]
public virtual User FolloweeUser { get; set; }
}
public class User
{
...
[InverseProperty("FollowedUser")]
public virtual ICollection<UserUserWatchListItem> FollowedUsers { get; set; }
[InverseProperty("FolloweeUser")]
public virtual ICollection<UserUserWatchListItem> FolloweeUsers { get; set; }
}

Edit multiple related tables on ASP.net MVC w/Entity Framework 6

I'll open with the statement that I am very new to .net and MVC so please bear with me. I'm using Visual Studio 2013 and learning as I go.
Essentially - I have a .net MVC database-first project connected to a SQL db. I used scaffolding to create 4 models -
(Survey_Header_Response) - base survey information, identifies which survey a respondent gets
(Survey_Question) holds a unique list of the questions for all surveys and provides the actual question text,
(Survey_Response) lists of all the questions in the survey identified in (Survey_Response_Header) and will hold the value of each answer on post,
(Response_Values) - holds a list of possible responses for each of the different questions in each survey . Information on these is as follows:
Note - models are scaffolded, so even if I change them, they change back on db update.
Survey_Response_Header model:
public partial class Survey_Response
{
public Survey_Response()
{
this.Response_Values = new HashSet<Response_Values>();
}
public int Survey_Response_RecID { get; set; }
public int Survey_RecID { get; set; }
public int Survey_Question_RecID { get; set; }
public string Response { get; set; }
public Nullable<System.DateTime> Date_Responded { get; set; }
public int Contact_RecID { get; set; }
public int Company_RecID { get; set; }
public System.DateTime Date_Sent { get; set; }
public bool Responded { get; set; }
public string Survey_Qtr { get; set; }
public System.Guid Respondent_ID { get; set; }
public virtual Survey_Detail Survey_Detail { get; set; }
public virtual Survey_Question Survey_Question { get; set; }
public virtual ICollection<Response_Values> Response_Values { get; set; }
public virtual Survey_Response_Header Survey_Response_Header { get; set; }
}
Survey_Question:
public partial class Survey_Question
{
public Survey_Question()
{
this.Survey_Cat_SubCat = new HashSet<Survey_Cat_SubCat>();
this.Survey_Detail = new HashSet<Survey_Detail>();
this.Response_Values = new HashSet<Response_Values>();
this.Survey_Response = new HashSet<Survey_Response>();
}
public int Survey_Question_RecID { get; set; }
public string Question { get; set; }
public bool Inactive_Flag { get; set; }
public System.DateTime Date_Created { get; set; }
public string Created_By { get; set; }
public System.DateTime Date_Updated { get; set; }
public string Updated_By { get; set; }
public virtual ICollection<Survey_Cat_SubCat> Survey_Cat_SubCat { get; set; }
public virtual ICollection<Survey_Detail> Survey_Detail { get; set; }
public virtual ICollection<Response_Values> Response_Values { get; set; }
public virtual ICollection<Survey_Response> Survey_Response { get; set; }
}
Survey Response:
public Survey_Response()
{
this.Response_Values = new HashSet<Response_Values>();
}
public int Survey_Response_RecID { get; set; }
public int Survey_RecID { get; set; }
public int Survey_Question_RecID { get; set; }
public string Response { get; set; }
public Nullable<System.DateTime> Date_Responded { get; set; }
public int Contact_RecID { get; set; }
public int Company_RecID { get; set; }
public System.DateTime Date_Sent { get; set; }
public bool Responded { get; set; }
public string Survey_Qtr { get; set; }
public System.Guid Respondent_ID { get; set; }
public virtual Survey_Detail Survey_Detail { get; set; }
public virtual Survey_Question Survey_Question { get; set; }
public virtual ICollection<Response_Values> Response_Values { get; set; }
public virtual Survey_Response_Header Survey_Response_Header { get; set; }
}
Response_Values:
public partial class Response_Values
{
public Response_Values()
{
this.Survey_Response = new HashSet<Survey_Response>();
}
public int Survey_RecID { get; set; }
public int Survey_Question_RecID { get; set; }
public int Value { get; set; }
public int Question_Type_RecID { get; set; }
public string Value_Label { get; set; }
public Nullable<System.DateTime> Date_Created { get; set; }
public string Created_By { get; set; }
public Nullable<System.DateTime> Date_Updated { get; set; }
public string Updated_By { get; set; }
public int Response_Value_RecID { get; set; }
public virtual Question_Type Question_Type { get; set; }
public virtual Survey_Question Survey_Question { get; set; }
public virtual Survey Survey { get; set; }
public virtual Survey_Detail Survey_Detail { get; set; }
public virtual ICollection<Survey_Response> Survey_Response { get; set; }
}
}
There is a many-to-many relationship between the Response_Values & Survey_Response tables through the use of a pure-join table not shown here.
ViewModels: (See edit below)
ResponseData (intended to hold Survey_Response data and reference related tables)
I apologize for the length of this question - I'm new at this so my coding is probably messy and my explanation long. Any help provided is much appreciated and will help me learn!
Edit:
Thanks for your reply. I understand where you're coming from, and I've attempted to build the controller but when I try to populate the ResponseData viewModel that contains the ICollection Survey_Response with data, I get an error "Cannot implicitly convert type 'System.Collections.Generic.List CustomerExperienceSurveyWeb.Models.Survey_Response' to 'System.Collections.Generic.ICollection CustomerExperienceSurveyWeb.ViewModels.SurveyResponseVM'. An explicit conversion exists (are you missing a cast?)"
Here's the relevant part of my controller code:
public ActionResult Edit(Guid id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
//Survey_Response_Header survey = db.Survey_Response_Header.Find(id);
var survey = db.Survey_Response_Header
.Include(i => i.Survey_Response)
.Where(i => i.Responded_ID == id)
.Select(i => new
{
ViewModel = new ResponseData
{
Responded_ID = i.Responded_ID,
Company_RecID = i.Company_RecID,
Contact = i.Contact,
Contact_RecID = i.Contact_RecID,
Date_Responsed = i.Date_Responsed,
Date_Sent = i.Date_Sent,
Responded = i.Responded,
Survey_Qtr = i.Survey_Qtr,
Survey_RecID = i.Survey_RecID,
SurveyResponse = i.Survey_Response.ToList() <<--- This is where the error shows
}
})
.Single();
Updated ResponseData viewModel:
public partial class ResponseData
{
public ResponseData()
{
this.SurveyResponse = new List<SurveyResponseVM>();
}
public System.Guid Responded_ID { get; set; }
public int Survey_RecID { get; set; }
public System.DateTime Date_Sent { get; set; }
public Nullable<System.DateTime> Date_Responsed { get; set; }
public bool Responded { get; set; }
public int Contact_RecID { get; set; }
public int Company_RecID { get; set; }
public string Survey_Qtr { get; set; }
public virtual Contact Contact { get; set; }
public virtual ICollection<SurveyResponseVM> SurveyResponse { get; set; }
}
Referenced SurveyResponseVM viewModel which is throwing the error:
public partial class SurveyResponseVM
{
public SurveyResponseVM()
{
this.Response_Values = new List<ValueData>();
}
public int Survey_Response_RecID { get; set; }
public int Survey_RecID { get; set; }
public int Survey_Question_RecID { get; set; }
public string Response { get; set; }
public Nullable<System.DateTime> Date_Responded { get; set; }
public int Contact_RecID { get; set; }
public int Company_RecID { get; set; }
public System.DateTime Date_Sent { get; set; }
public bool Responded { get; set; }
public string Survey_Qtr { get; set; }
public System.Guid Respondent_ID { get; set; }
public virtual Survey_Detail Survey_Detail { get; set; }
public virtual Survey_Question Survey_Question { get; set; }
public virtual ICollection<ValueData> Response_Values { get; set; }
public virtual Survey_Response_Header Survey_Response_Header { get; set; }
}
I know this means I'm not populating the ICollection part of the viewmodel correctly but I can't seem to figure out how it's supposed to be done. I've done days of research on the internet and I either don't know the right question to ask or I'm completely missing it. Any help you can give me is VERY appreciated!
So the controller is essentially responsible for populating a model and pass this model to a view for rendering, there doesn't need to correspond to a database table.
The way I normally tackle this is to look at what the function is being performed, in this case survey and create a SurveyController. In here you will have a bunch of actions that correspond to views that are responsible for retrieving the correct data from the database, populating a model and then passing the model to the view for rendering.
So if you wanted to display a list of questions, you may do something like this (apologies if this contains errors, no VS atm):
public class SurveyController : Controller
{
public ActionResult Index()
{
var model = new SurveyModel(); // This would contain any properties, like questions and their valid responses
return View(model);
}
}
public class SurveyModel
{
public IList<SurveyQuestionModel> Questions { get; set; }
}

EF6 MVC5 Setting a 1-1 Relationship

I have got my application up and running using Code first, I am trying to set a 1-1 relationship but when I update-database I get the error "SupplyPointId: Name: Each property name in a type must be unique. Property name 'SupplyPointId' is already defined."
I've tried removing the existing index constraint on SupplyPointAddress.SupplyPointId and that does not help. In the other table its the PK. Any comments really appreciated
public partial class SupplyPoint
{
[Key]
//[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SupplyPointId { get; set; }
public string SPID { get; set; }
public string SupplyPointName { get; set; }
public int SupplyPointTypeId { get; set; }
public DateTime SupplyPointEffectiveDateTime { get; set; }
public string GazateerRef { get; set; }
public virtual SupplyPointType SupplyPointType { get; set; }
//[ForeignKey("SupplyPointId")]
public virtual SupplyPointAddress SupplyPointAddress { get; set; }
}
public partial class SupplyPointAddress
{
[Key]
//[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SupplyPointAddressId { get; set; }
public int SupplyPointId { get; set; }
public string D5001_FreeDescriptor { get; set; }
public string D5002_SubBuildingName { get; set; }
public string D5003_BuildingName { get; set; }
public string D5004_BuildingNumber { get; set; }
public string D5005_DependentThoroughfareName { get; set; }
public string D5006_DependentThoroughfareDescriptor { get; set; }
public string D5007_ThoroughfareName { get; set; }
public string D5008_ThoroughfareDescriptor { get; set; }
public string D5009_DoubleDependentLocality { get; set; }
public string D5010_DependentLocality { get; set; }
public string D5011_PostTown { get; set; }
public string D5012_County { get; set; }
public string D5013_Postcode { get; set; }
public virtual SupplyPoint SupplyPoint { get; set; }
}
public System.Data.Entity.DbSet<AscendancyCF.Models.SupplyPoint> SupplyPoints { get; set; }
public System.Data.Entity.DbSet<AscendancyCF.Models.SupplyPointAddress> SupplyPointAddresses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<SupplyPointAddress>()
.HasOptional<SupplyPoint>(u => u.SupplyPoint)
.WithRequired(c => c.SupplyPointAddress).Map(p => p.MapKey("SupplyPointId"));
base.OnModelCreating(modelBuilder);
}
I moved the foreign key into SupplyPoint table so that the foreign key was being defined as SupplyPointAddressId in SupplyPoint. This worked and allows me to do SupplyPoint.SupplyPointAddress in resultant model
Since you're testing with a real DB. Use some of the
Database Initialization Strategies in Code-First:
public class SchoolDBContext: DbContext
{
public SchoolDBContext(): base("SchoolDBConnectionString")
{
Database.SetInitializer<SchoolDBContext>(new CreateDatabaseIfNotExists<SchoolDBContext>());
//Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseIfModelChanges<SchoolDBContext>());
//Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseAlways<SchoolDBContext>());
//Database.SetInitializer<SchoolDBContext>(new SchoolDBInitializer());
}
public DbSet<Student> Students { get; set; }
public DbSet<Standard> Standards { get; set; }
}
(Excerpt from this site)
It is pretty self explanatory.
If there's already a DB created, it just DROPs it.
Happy coding!

Entity Framework WebApi Circular dependency serialization error

I think, I've read everything about this error and I tried everything. Here are my models:
Main:
public class Trip
{
public int TripId { get; set; }
public string Name { get; set; }
public string ShortDescription { get; set; }
public string Country { get; set; }
public float BasicPrice { get; set; }
public virtual ICollection<ApartmentType> ApartmentType { get; set; }
public virtual ICollection<TransportMethod> TransportMethod { get; set; }
public virtual ICollection<FeedingType> FeedingType { get; set; }
}
ApartmentType:
public class TransportMethod
{
public int TransportMethodId { get; set; }
public int TripId { get; set; }
public string Name { get; set; }
public float Price { get; set; }
}
FeedingType:
public class FeedingType
{
public int FeedingTypeId { get; set; }
public int TripId { get; set; }
public string Description { get; set; }
public float Price { get; set; }
}
TransportType:
public class TransportMethod
{
public int TransportMethodId { get; set; }
public int TripId { get; set; }
public string Name { get; set; }
public float Price { get; set; }
}
When serializng the Trip entity I get a circular dependency error. Things i tried:
Disable lazy loading in DbContext.
Adding
json.SerializerSettings.PreserveReferencesHandling=Newtonsoft.Json.PreserveReferencesHandling.All; to GLobal.asax
Adding a decorator [IgnoreDataMember] to TripId in every child entity.
Mapping this entity to a ViewModel which doesn't contain the ICollection members. - This worked ok, but at some point I will want to get those lists to the client.
I really don't know what's going on. What am I missing? I really can't spot any circular dependency.
Have you tried adding the [JsonIgnore] attribute to the TripId to the children entities?
http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_JsonIgnoreAttribute.htm
or setting
json.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

Resources