I am trying to insert value in DateTime column with Entity Framework but it throwing error on db.SaveChanges() function. There are 2 DateTime columns but exception throwing for only one (entry.OT_Date) with no maximum detail.
var entry = new OT_Request_N();
entry.Emp_ID = StaticFunctions.WindowUserId;
entry.slno = db.OT_Request_N.Max(x=>x.slno)+1;
entry.OT_Date= otD;
entry.OT_Time_Req = hourRequest;
entry.Remarks_Req = sError.ReqRemarks;
entry.Req_Sent_On = DateTime.Now;
entry.Supervisor_ID = sError.ManagerID;
entry.Approve_Status = 0;
db.OT_Request_N.Add(entry);
db.SaveChanges();
Here is SQL table Design
Here is class generated in edmx file
public partial class OT_Request_N
{
public int slno { get; set; }
public string Emp_ID { get; set; }
public Nullable<System.DateTime> Req_Sent_On { get; set; }
public Nullable<int> OT_Time_Req { get; set; }
public string Supervisor_ID { get; set; }
public string Remarks_Req { get; set; }
public string Remarks_App { get; set; }
public Nullable<int> OT_Time_App { get; set; }
public string Approved_By { get; set; }
public Nullable<int> Approve_Status { get; set; }
public Nullable<System.DateTime> Approved_On { get; set; }
public Nullable<int> Operations { get; set; }
public string AltReq_ID { get; set; }
public Nullable<bool> Is_alert_sent { get; set; }
public Nullable<System.DateTime> OT_Date { get; set; }
}
Related
I was making an API for saving a model where it has one to many relationship with another model. When I applying automapping in it. it is giving me following error:
CreateContestDto -> Contest
tritronAPI.DTOs.CreateContestDto -> tritronAPI.Model.Contest
Type Map configuration:
CreateContestDto -> Contest
tritronAPI.DTOs.CreateContestDto -> tritronAPI.Model.Contest
Destination Member:
Problems
---> AutoMapper.AutoMapperMappingException: Missing type map
configuration or unsupported mapping.
Mapping types:
ProblemDto -> Problem
tritronAPI.DTOs.ProblemDto -> tritronAPI.Model.Problem
at lambda_method(Closure , ProblemDto , Problem , ResolutionContext )
My models are: Contest and Problem a contest contain many problems:
public class Contest
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public ICollection<Problem> Problems { get; set; }
public ICollection<ContestProgrammingLanguage>
ContestProgrammingLanguages { get; set; }
}
public class Problem
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[MaxLength(255)]
public string ProblemName { get; set; }
[ForeignKey("User")]
public string ProblemAuthorId { get; set; }
public virtual User ProblemAuthor { get; set; }
public string AuthorName { get; set; }
//public virtual List<Resources> Resourceses { get; set; }
public string ProblemDescription { get; set; }
public bool IsPublished { get; set; }
public virtual ICollection<Submission> Submissions { get; set; }
public string Tags { get; set; }
//public Guid Contest_Id { get; set; }
public virtual Contest Contest { get; set; }
[ForeignKey("Contest")]
public int? Contest_Id { get; set; }
public short Score { get; set; }
//Timelimit in miliseconds
public int TimeLimit { get; set; }
//MemoryLimit in bytes
public int MemoryLimit { get; set; }
//More than source code limit is not allowed
public int? SourceCodeLimit { get; set; }
public virtual ICollection<TestFile> TestFiles { get; set; } = new
List<TestFile>();
}
public class CreateContestDto
{
public CreateContestDto()
{
this.Problems = new HashSet<ProblemDto>();
}
public string Name { get; set; }
public DateTime StartDate { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndDate { get; set; }
public DateTime EndTime { get; set; }
public string BackgroundImage { get; set; }
public string Description { get; set; }
public ICollection<ProblemDto> Problems { get; set; }
}
public class ProblemDto
{
public int Id { get; set; }
public string ProblemName { get; set; }
}
mapping profile:
CreateMap<CreateContestDto, Contest>().ForMember(
dest => dest.Problems , opt => opt.MapFrom(src =>
src.Problems));
controller code:
public async Task<IActionResult> AddContest([FromBody]
CreateContestDto contest)
{
var con = _mapper.Map<Contest>(contest);
this._uow.ContestRepository.Add(con);
return Ok();
}
I have already tried with reversemap selecting new id in mapping profile
You also need to add mappings for ProblemDTO to Problem:
CreateMap<CreateContestDto, Contest>();
CreateMap<ProblemDto, Problem>();
This is my error :
Unable to determine the principal end of an association between the types 'SANTEK.Web.Models.SantekOrder' and 'SANTEK.Web.Models.OrderMoneySum'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
What is my mistake logically ?? I mean to build one-to-one relation.
My model classes are here :
[Table("Orders")]
public class SantekOrder
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SantekOrderId { get; set; }
[Required, StringLength(25)]
[DisplayName("Çatdırılacaq şəxs")]
public string DelivaryManName { get; set; }
[DisplayName("Çatdırılacaq şəxs istifadəçidir?")]
public bool DeliveryManIsUser { get; set; }
[Required, StringLength(25)]
[DisplayName("Çatdırılacaq şəxs ad və soyadı")]
public string DelivaryManSurname { get; set; }
[Required, StringLength(10)]
[DisplayName("Çatdırılacaq şəxs tefon nömrəsi")]
public string DelivaryManTel { get; set; }
[DisplayName("Çatdırılacaq ünvan istifadəçinin ünvandır?")]
public bool IsUserAddress { get; set; }
[Required, StringLength(150)]
[DisplayName("Çatdırılacaq şəxsin ünvanı?")]
public string DelivaryManAddress { get; set; }
public int SanTekUserId { get; set; }
public int OrderMoneySumId { get; set; }
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy-hh-mm-ss}",
ApplyFormatInEditMode = true)]
public DateTime Time { get; set; }
[ForeignKey("SanTekUserId")]
public virtual SanTekUser SanUser { get; set; }
[ForeignKey("SanTekUserId")]
public virtual OrderMoneySum OrderMoneySum { get; set; }
}
This is my OrdersDetail Table to hold all product and their quantity
[Table("OrderedProducts")]
public class OrderMoneySum
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int OrderMoneySumId { get; set; }
[Required]
public int SantekOrderId { get; set; }
[Required]
public int AllProductId { get; set; }
[DisplayName("Miqdarı")]
public int Quantity { get; set; }
[ForeignKey("SantekOrderId")]
public virtual SantekOrder SantekOrder { get; set; }
[ForeignKey("SantekOrderId")]
public virtual AllProduct AllProduct { get; set; }
}
Developing Asp .Net MVC web application. Using Entity framework 6.1.1 and Visual Studio 2013.
Got this Linq:
Db.Distrule_contents.Where(x => x.distrule_id == DistruleId && x.Content.season_id.HasValue && x.Content.season.series_id == SeriesId).Select(x => x.Content).ToList();
and associated with DB tables classes:
[Table("distrule_content")]
public class Distrule_content
{
[Key,Column(Order=0)]
public int distrule_id { get; set; }
[Key, Column(Order = 1)]
[ForeignKey("Content")]
public string IDEC { get; set; }
public int status_id { get; set; }
public virtual Distrule Distrule { get; set; }
public virtual Content Content { get; set; }
public virtual Status Status { get; set; }
}
[Table("distrule")]
public class Distrule: CommonEntity
{
public string distrule_name { get; set; }
public DateTime? begin_date { get; set; }
public DateTime end_date { get; set; }
public int status_id { get; set; }
public int? minutes_to_tx { get; set; }
public string period_type { get; set; }
public bool? autoactivate_content { get; set; }
public virtual ICollection<Distrule_area> DistruleAreas { get; set; }
public virtual ICollection<Distrule_content> DistruleContent { get; set; }
public virtual ICollection<Distrule_georegion> DistruleGeoregion { get; set; }
public virtual Status status { get; set; }
}
[Table("content")]
public class Content
{
[Key]
public string IDEC { get; set; }
public DateTime? date_inserted { get; set; }
public DateTime? min_tx_date { get; set; }
public long? season_id { get; set; }
public int? episode_number { get; set; }
public string content_rus_name { get; set; }
public virtual Season season { get; set; }
}
[Table("status")]
public class Status: CommonEntity
{
public string status_name { get; set; }
}
As a Result receave this SQL Query:
SELECT
[Filter1].[distrule_id1] AS [distrule_id],
[Filter1].[IDEC1] AS [IDEC],
[Filter1].[date_inserted1] AS [date_inserted],
[Filter1].[min_tx_date1] AS [min_tx_date],
[Filter1].[season_id1] AS [season_id],
[Filter1].[episode_number1] AS [episode_number],
[Filter1].[content_rus_name1] AS [content_rus_name],
[Filter1].[season_id2] AS [season_id1]
FROM ( SELECT [Extent1].[distrule_id] AS [distrule_id1], [Extent1].[Distrule_Id] AS [distrule_id1], [Extent2].[IDEC] AS [IDEC1], [Extent2].[date_inserted] AS [date_inserted1], [Extent2].[min_tx_date] AS [min_tx_date1], [Extent2].[season_id] AS [season_id1], [Extent2].[episode_number] AS [episode_number1], [Extent2].[content_rus_name] AS [content_rus_name1], [Extent2].[season_Id] AS [season_id1], [Extent3].[series_id] AS [series_id1], [Extent3].[Series_Id] AS [series_id1], [Extent4].[season_id] AS [season_id2], [Extent4].[season_Id] AS [season_id2]
FROM [dbo].[distrule_content] AS [Extent1]
INNER JOIN [dbo].[content] AS [Extent2] ON [Extent1].[IDEC] = [Extent2].[IDEC]
LEFT OUTER JOIN [dbo].[Season] AS [Extent3] ON [Extent2].[season_Id] = [Extent3].[Id]
LEFT OUTER JOIN [dbo].[content] AS [Extent4] ON [Extent1].[IDEC] = [Extent4].[IDEC]
WHERE [Extent2].[season_id] IS NOT NULL
) AS [Filter1]
WHERE ([Filter1].[distrule_id1] = #p__linq__0) AND ([Filter1].[series_id1] = #p__linq__1)
And following error: The column 'distrule_id1' was specified multiple times for 'Filter1'.
What i am doing wrong?
update 2014-09-10
Solved after deep study EF navigation rules. Thx to chconger. Problem was in letting EF self realize where FK should be placed. So after adding everywhere ForeignKey attribute is worked fine.
Here how it looks now:
[Table("distrule_content")]
public class Distrule_content
{
[Key,Column(Order=0)]
public int distrule_id { get; set; }
[Key, Column(Order = 1)]
[ForeignKey("Content")]
public string IDEC { get; set; }
public int status_id { get; set; }
[ForeignKey("distrule_id")]
public virtual Distrule Distrule { get; set; }
[ForeignKey("IDEC")]
public virtual Content Content { get; set; }
[ForeignKey("status_id")]
public virtual Status Status { get; set; }
}
Couple of issues from what I can see.
1. You have a foreign key set above a mismatched string IDEC, instead
[Key, Column(Order = 1)]
[ForeignKey("Content")]
public virtual Content Content { get; set; }
2. You are missing a constructor with HashSets for your class Distrule
public Distrule()
{
DistruleAreas = new HashSet<DistruleArea>();
DistruleContents = new HashSet<DistruleContent>();
DistruleGeoregions = new HashSet<DistruleGeregion>();
}
I'm deploying a Adventure Cycle page where the people can order one of bikes. I want to create a Shipping Information include (Address 1, Address 2, City, Country, Zip/Postal, and State/Province). When click the Submit Order it will redirect to Complete page to inform User that order is successful.
Address model:
public partial class Address
{
public Address()
{
this.SalesOrderHeaders = new HashSet<SalesOrderHeader>();
this.SalesOrderHeaders1 = new HashSet<SalesOrderHeader>();
}
public int AddressID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public Nullable<int> StateProvinceID { get; set; }
public string PostalCode { get; set; }
public System.Guid rowguid { get; set; }
public Nullable<System.DateTime> ModifiedDate { get; set; }
public virtual StateProvince StateProvince { get; set; }
public virtual ICollection<SalesOrderHeader> SalesOrderHeaders { get; set; }
public virtual ICollection<SalesOrderHeader> SalesOrderHeaders1 { get; set; }
}
Sales Order Header:
public partial class SalesOrderHeader
{
public SalesOrderHeader()
{
this.SalesOrderDetails = new HashSet<SalesOrderDetail>();
}
public int SalesOrderID { get; set; }
public byte RevisionNumber { get; set; }
public Nullable<System.DateTime> OrderDate { get; set; }
public Nullable<System.DateTime> DueDate { get; set; }
public Nullable<System.DateTime> ShipDate { get; set; }
public byte Status { get; set; }
public bool OnlineOrderFlag { get; set; }
public string SalesOrderNumber { get; set; }
public string PurchaseOrderNumber { get; set; }
public string AccountNumber { get; set; }
public int CustomerID { get; set; }
public Nullable<int> SalesPersonID { get; set; }
public Nullable<int> TerritoryID { get; set; }
public Nullable<int> BillToAddressID { get; set; }
public Nullable<int> ShipToAddressID { get; set; }
public int ShipMethodID { get; set; }
public Nullable<int> CreditCardID { get; set; }
Hope it will help you,
On your Complete View you must put the model Address to the redirect method of the Complet e form:
Like
`Public ActionResult SubmitOrder(Address address)
{
...
return View("Complete", address);
}
`
and
on your Complete View
first line must be
`
#Model namespace.Model.Address
//then you can use
#model.AddressLine1
#model.AddressLine2
#model.City
#model.PostalCode
#model.StateProvinceID // You will retrieve an Id here I guess but at least you will
//have your data. embedded in your razor code
`
I a am new to entity framework. I have created project with Entity Model. When i am trying to add item in to the table it gives error "Multiplicity Constraint violated" Any idea?
ID From Table MAPPER is FK in MAPS table.
In Code Behind:
private Mapper mMapper;
if (mMapper.Id == 0)
db.Mappers.Add(mMapper);
db.SaveChanges();
public partial class Mapper
{
public Mapper()
{
this.Maps = new HashSet<Map>();
}
public int Id { get; set; }
public string Name { get; set; }
public string RecordDelimiter { get; set; }
public string ValueDelimiter { get; set; }
public Nullable<int> ColumnDescriptionRow { get; set; }
public Nullable<int> FirstRowOfData { get; set; }
public Nullable<bool> IgnoreThisRecord { get; set; }
public Nullable<bool> ExplodePartQuantity { get; set; }
public Nullable<bool> IgnoreImportingErrors { get; set; }
public Nullable<int> AngleMappingLeadingEdgeId { get; set; }
public Nullable<int> AngleMappingTrailingEdgeId { get; set; }
public string ValueWrapper { get; set; }
public string ValueWrapperDelimiter { get; set; }
public virtual AngleMapping AngleMapping { get; set; }
public virtual AngleMapping AngleMapping1 { get; set; }
public virtual ICollection<Map> Maps { get; set; }
}
Help Appreciated!