Hello everybody and thanks for reading and tryin to help.
I'm a beginner in asp.
I Have two classes :
public class Organisation
{
public int OrganisationId { get; set; }
public string Nom_Organisation { get; set; }
public string Adresse_Organisation { get; set; }
public string Mail_Organisation { get; set; }
public DateTime Date_Ajout_Org { get; set; }
public virtual ICollection<Contrat> contract { get; set; }
public virtual ICollection<Ticket> Ticket_Org { get; set; }
public virtual ICollection<Interlocuteur> Interlocuteurs { get; set; }
public virtual ICollection<Environnement> Environments { get; set; }
public virtual ICollection<Outils> Outils { get; set; }
public virtual ICollection<Support> Supports { get; set; }
public Organisation()
{
}
}
and
public class Contrat
{
public int ContratId { get; set; }
public DateTime Date_Conclu { get; set; }
public string Reference { get; set; }
public string Commentaire { get; set; }
public virtual Organisation Orga{ get; set; }
[Required]
public int TypeContratId { get; set; }
public virtual TypeContrat Type { get; set; }
public Contrat()
{
}
}
I want to create an Organisation then add to it the contract. The thing is that i really don't see how. I create the Organisation with the generated controller but when i try to think about a way to create the Contract by sending to it the Organisation ID.
I want to add to the default generated table a button that send to the action that create the contract with the ID of the organisation selected.
Hope that u've understood my question! Thanks in advance.
I think that you use Entities Framework. If so, then you need not worry about it
just create Organisation and to add Contrat in ICollection<Contrat> contract
Organisation newOrg = new Organisation();
newOrg.contract = new List<Contrat>();
newOrg.contract.add(new contract())
context.Organisation.Add(newOrg);
context.save();
Or you can use Guid how other way:
change to:
public int OrganisationId { get; set; } => public Guid OrganisationId { get; set; }
Organisation newOrg = new Organisation();
newOrg.OrganisationId = Guid.New();
contract newContract = new contract();
newContract.OrganisationId = newOrg.OrganisationId;
context.Organisation.Add(newOrg);
context.save();
Related
I have got the following three tables (just an example)
public class User
{
public int id { get; set; }
public string name { get; set; }
public List<Code> Codes { get; set; }
}
public class Device
{
public int id { get; set; }
public string name { get; set; }
public List<Code> Codes { get; set; }
}
public class Code
{
public int id { get; set; }
public int entity_id { get; set; }
public string entity_type { get; set; }
public string code { get; set; }
}
Now a code can either belong to a User or a Device, which will be determined by the value of entity_type (i.e. 'user' or 'device'). How can this be achieved in Entity Framework ?
You could change your Code class to
public class Code
{
public int id { get; set; }
public User user { get; set; }
public Device device { get; set; }
public string code { get; set; }
}
This way one of those properties can be null.
Hope it helps.
The drawback is that you could have a code that have a user an a device
In simple relational terms, I want each entry of ContractDetails to be assigned to either a Site OR a Company, not both at the same time, and one of them must be selected or there is no link at all. I'm not quite sure how to represent this in entity framework. My Model at Present:
Company Model:
public class Company
{
public int ID { get; set; }
public string Company_Name { get; set; }
public string Company_Prefix { get; set; }
public virtual ICollection<Site> Sites { get; set; }
}
Contract Details Model:
public class ContractDetails
{
public int ContractDetailsID { get; set; }
public int ContractTypeID { get; set; }
public int ContractRenewalPeriodID { get; set; }
public int? CompanyID { get; set; }
public int? SiteID { get; set; }
[Required, StringLength(10)]
public string Reference { get; set; }
[Display(Name = "Contract Start Date"), DataType(DataType.Date)]
public DateTime? Contract_Start_Date { get; set; }
[Display(Name = "Contract End Date"), DataType(DataType.Date)]
public DateTime? Contract_End_Date { get; set; }
[Column(TypeName = "text")]
public string Notes { get; set; }
public string Direct_Debit_Reference { get; set; }
public virtual Company Company { get; set; }
public virtual Site Site { get; set; }
public virtual ContractType ContractType { get; set; }
public virtual ContractRenewalPeriod ContractRenewalPeriod { get; set; }
}
Site Model:
public class Site
{
public int ID { get; set; }
public string Site_Name { get; set; }
public string Site_TelephoneNumber { get; set; }
public string Site_City { get; set; }
public int CompanyID { get; set; }
public virtual Company Company { get; set; }
}
Just implement the IValidatableObject on ContractDetails class. On Validate method put the validation logic. If the object is not valid you must return a collection of ValidationResult. when saving the object, EF will execute the Validate method and verify that your ContractDetails object is coherent.
I have the following classes, a challenge has many submissions and it also has a winner.
The problem is that in my database Entity framework has created two columns on the Submissions table [ChallengeId] & [Challenge_ChallengeId] when I save an object it is added to the ChallengeId Column but relationship is being held on the Challenge_ChallengeId column.
Is there an issue with my models that is causing this or is some way I can set to hold the relationship and save to the same column?
Thanks!
public class Submission
{
[Key]
public Int32 SubmissionId { get; set; }
public DateTime Created { get; set; }
public Int32 ChallengeId { get; set; }
[ForeignKey("ChallengeId")]
public virtual Challenge Challenge { get; set; }
public String YouTubeVideoCode { get; set; }
public virtual IList<SubmissionVote> Votes { get; set; }
}
public class Challenge
{
[Key]
public Int32 ChallengeId { get; set; }
[Required]
public String ChallengeName { get; set; }
public virtual IList<Submission> Submissions { get; set; }
public Int32? OverallWinnerId { get; set; }
[ForeignKey("OverallWinnerId")]
public virtual Submission OverallWinner { get; set; }
}
I think you could benefit from just using the conventions and also using the shorthand keywords as data types. I think the error is because you have annotated the proxy with a ForeignKey attribute. Try this
public class Submission
{
public int SubmissionId { get; set; }
public DateTime Created { get; set; }
public int ChallengeId { get; set; }
public string YouTubeVideoCode { get; set; }
public virtual Challenge Challenge { get; set; }
public virtual IList<SubmissionVote> Votes { get; set; }
}
public class Challenge
{
public int ChallengeId { get; set; }
[Required]
public string ChallengeName { get; set; }
public int? OverallWinnerId { get; set; }
public virtual Submission OverallWinner { get; set; }
public virtual IList<Submission> Submissions { get; set; }
}
I'm pretty sure that Entity Framework uses a convention based approach, and the easiest way to fix this is to use the Id name instead of ChallengeId and SubmissionId
public class Submission
{
[Key]
public Int32 Id { get; set; }
public DateTime Created { get; set; }
public Int32 ChallengeId { get; set; }
[ForeignKey("ChallengeId")]
public virtual Challenge Challenge { get; set; }
public String YouTubeVideoCode { get; set; }
public virtual IList<SubmissionVote> Votes { get; set; }
}
public class Challenge
{
[Key]
public Int32 Id { get; set; }
[Required]
public String ChallengeName { get; set; }
public virtual IList<Submission> Submissions { get; set; }
public Int32? OverallWinnerId { get; set; }
[ForeignKey("OverallWinnerId")]
public virtual Submission OverallWinner { get; set; }
}
I am trying to understand how to create a ViewModel that contains properties from a class in my domain model as well as properties from a parent class.
I want to have a ViewModel that contains all of the LoadSession properties and the TradingPartner Description, but I'm not sure how to map this all up in the ViewModel. Any help or advice would be greatly appreciated.
This is my main class I'm accessing named LoadSession:
public partial class LoadSession
{
public LoadSession()
{
this.AcceptedTransactions = new HashSet<AcceptedTransaction>();
this.RejectedTransactions = new HashSet<RejectedTransaction>();
}
public int LoadSessionId { get; set; }
public int Import { get; set; }
public string FilePath { get; set; }
public string TradingPartnerBatchId { get; set; }
public System.DateTime Started { get; set; }
public int RecordsOnFile { get; set; }
public int RecordsAfterGroupFilter { get; set; }
public int RecordsAccepted { get; set; }
public int RecordsRejected { get; set; }
public System.DateTime Completed { get; set; }
public bool Success { get; set; }
public Nullable<int> Extract { get; set; }
public virtual ICollection<AcceptedTransaction> AcceptedTransactions { get; set; }
public virtual Extract Extract1 { get; set; }
public virtual Import Import1 { get; set; }
public virtual ICollection<RejectedTransaction> RejectedTransactions { get; set; }
}
The Import property is a foreign key for this Import class (Import = ImportId):
public partial class Import
{
public Import()
{
this.GroupPlans = new HashSet<GroupPlan>();
this.ImportGroups = new HashSet<ImportGroup>();
this.MatchingGroups = new HashSet<MatchingGroup>();
this.LoadSessions = new HashSet<LoadSession>();
}
public int ImportId { get; set; }
public string Description { get; set; }
public int Format { get; set; }
public int Interface { get; set; }
public virtual Interface Interface1 { get; set; }
public virtual Format Format1 { get; set; }
public virtual ICollection<GroupPlan> GroupPlans { get; set; }
public virtual ICollection<ImportGroup> ImportGroups { get; set; }
public virtual ICollection<MatchingGroup> MatchingGroups { get; set; }
public virtual ICollection<LoadSession> LoadSessions { get; set; }
}
The Interface property is a foreign key for this Interface class (Interface = InterfaceId):
public partial class Interface
{
public Interface()
{
this.Extracts1 = new HashSet<Extracts1>();
this.Imports = new HashSet<Import>();
}
public int InterfaceId { get; set; }
public string Description { get; set; }
public int TradingPartner { get; set; }
public virtual ICollection<Extracts1> Extracts1 { get; set; }
public virtual ICollection<Import> Imports { get; set; }
public virtual TradingPartner TradingPartner1 { get; set; }
}
And the TradingPartner property is a foreign key for this TradingPartner class (TradingPartner = TradingPartnerId):
public partial class TradingPartner
{
public TradingPartner()
{
this.Interfaces = new HashSet<Interface>();
}
public int TradingPartnerId { get; set; }
public string Description { get; set; }
public virtual ICollection<Interface> Interfaces { get; set; }
}
Well, those are all your domain objects right...
Create a repository that takes your Domain object and transforms then to into a view model with the properties you need...
I am not sure what it is your view needs, but from your statements you state that you want properties of Load session + TradingPartner.Description So create something like this...
public class LoadSessionTradingPrtNrVM
{
public LoadSession()
{
this.AcceptedTransactions = new HashSet<AcceptedTransaction>();
this.RejectedTransactions = new HashSet<RejectedTransaction>();
}
public int LoadSessionId { get; set; }
public int Import { get; set; }
public string FilePath { get; set; }
public string TradingPartnerBatchId { get; set; }
public System.DateTime Started { get; set; }
public int RecordsOnFile { get; set; }
public int RecordsAfterGroupFilter { get; set; }
public int RecordsAccepted { get; set; }
public int RecordsRejected { get; set; }
public System.DateTime Completed { get; set; }
public bool Success { get; set; }
public Nullable<int> Extract { get; set; }
public string Description { get; set; }
public virtual ICollection<AcceptedTransaction> AcceptedTransactions { get; set; }
public virtual Extract Extract1 { get; set; }
public virtual Import Import1 { get; set; }
public virtual ICollection<RejectedTransaction> RejectedTransactions { get; set; }
}
To get from Domain models to ViewModels you would use a repository, or some other pattern that takes what you get from your database and transforms it into what you need for your views.
This is kind of raw, but the theory should hold...
public class DataRepository {
LoadSessionTradingPrtNrVM TransformToVM(LoadSession inputA, TradingPartner inputB){
LoadSessionTradingPrtNrVM newOBJ = new LoadSessioNTradingPrtNrVM();
newOBJ.LoadSessionId = ipnutA.LoadSessionID;
newOBJ.Import = inputA.Import
//Here is the property from your Transform object
newOBJ.Description = inputB.Description
//... Continue to transform one object into the other...
//You could add as many members from as many different objects as you want into
//Your view model following that pattern.
}
}
I didn't have a chance to run this through a C# compiler, but you should get the general idea. I am sure there is a more elegant pattern that can accomplish the same thing. But this is a decent solution off the top of my head.
Another option is to include the domain model objects as properties in the view model. For example:
// View model.
public class UserViewModel
{
public AddressModel Address; // Assuming "AddressModel" is a doman model.
public string FirstName;
public string LastName;
}
And in the view you can access the properties as:
#Model.Address.AddressLine1
#Model.Address.City
// etc...
The Html helpers handle this just fine, but if you are manually naming inputs in your view don't forget to adjust those names to match.
This question already has answers here:
Why doesn't Include have any effect?
(2 answers)
Closed 1 year ago.
I'm trying to to do include and group in in one sentence
var instanceIdList = context.
Tracks.
Include("Services").
GroupBy(x => x.ServiceId).
Take(top);
but when I check the result at debug I cant see any of the include values
I tried to do in another way
var objectContext = ((IObjectContextAdapter)context).ObjectContext;
var set = objectContext.CreateObjectSet<Track>();
var instanceIdList = set.Include("Services").GroupBy(x => x.ServiceId);
this is the classes :
Track
public partial class Track
{
public long Id { get; set; }
public System.Guid ServiceId { get; set; }
public Nullable<System.Guid> ServiceInterfaceId { get; set; }
public Nullable<System.Guid> ProviderId { get; set; }
public System.Guid ServiceInstanceId { get; set; }
public System.Guid ActivityParentId { get; set; }
public System.Guid ActivityInstanceId { get; set; }
public int ActivityType { get; set; }
public int ServiceRole { get; set; }
public int TrackOrder { get; set; }
public System.DateTime Datetime { get; set; }
public Nullable<System.Guid> MessageId { get; set; }
public int Status { get; set; }
public Nullable<int> ESBErrorCode { get; set; }
public Nullable<int> ESBTecnicalErrorCode { get; set; }
public string ErrorDescription { get; set; }
public string PortName { get; set; }
public string MachineName { get; set; }
public string ConsumerId { get; set; }
public string ExternalId { get; set; }
public string ConsumerMachineName { get; set; }
public int ServiceBehavior { get; set; }
public virtual Message Message { get; set; }
}
Service
public partial class Service
{
public Service()
{
this.Providers = new HashSet<Provider>();
this.ServiceInterfaces = new HashSet<ServiceInterface>();
}
public System.Guid ServiceId { get; set; }
public string ServiceName { get; set; }
public string ServiceNumber { get; set; }
public Nullable<System.Guid> ModelSchemaId { get; set; }
public virtual ICollection<Provider> Providers { get; set; }
public virtual ICollection<ServiceInterface> ServiceInterfaces { get; set; }
}
but the result is the same
thanks
miki
You also need to put include in the end.
Like this...
var instanceIdList = context.Tracks
.GroupBy(x => x.ServiceId)
.Take(top)
.Include("Services");
You have not defined any navigation property for Services in your Track class, you need to add the following property.
public virtual ICollection<Service> Services { get; set; }
Your Track class has no member accessor called service, so
Include("Services")
won't work.
You need to link to Service from Track, e.g.
public Service Services {get;set;}