Entity Framework Code First Using One column as Primary Key and another as Auto Increment Column

You can also do this with Data Annotations: public class Sale { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Key] public string TrNo { get; set; } public DateTime Date { get; set; } public int CustomerID { get; set; } public ObservableCollection<SaleDetail> SaleDetails { get; set; } }

SQL LocalDB vs SQL Server CE

See the Introducing SQL Server Express Local DB Runtime presentation – gives a great overview. The huge benefit of LocalDB is that it’s real SQL Server – it’s a special version of SQL Server Express, but it basically supports everything that “real” SQL Server has – spatial data types, stored procedures – you name it. … Read more

Understanding ForeignKey attribute in entity framework code first

The required side of the 1..0 relationship MemberDataSet should not have a FK to DeferredData. Instead, DeferredData‘s PK should also be a FK to MemberDataSet (known as shared primary key) public class MemberDataSet { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual DeferredData DeferredData { get; set; } } public class DeferredData … Read more

How do I get the entity that represents the current user in Symfony?

Symfony 4+, 2019+ Approach In symfony 4 (probably 3.3 also, but only real-tested in 4) you can inject the Security service via auto-wiring in the controller like this: <?php use Symfony\Component\Security\Core\Security; class SomeClass { /** * @var Security */ private $security; public function __construct(Security $security) { $this->security = $security; } public function privatePage() : Response … Read more

Creating Reports in ASP.Net with Entity Framework

Below is a quick sample of how i set the report datasource in one of my .NET winForms applications. public void getMyReportData() { using (myEntityDataModel v = new myEntityDataModel()) { var reportQuery = (from r in v.myTable select new { l.ID, l.LeaveApplicationDate, l.EmployeeNumber, l.EmployeeName, l.StartDate, l.EndDate, l.Supervisor, l.Department, l.Col1, l.Col2, ……., ……., l.Address }).ToList(); reportViewer1.LocalReport.DataSources.Clear(); … Read more

Entity Framework – First query slow

We had the same issue in EF 5.0 and as of today a superficial Google search does not reveal a sufficient speed-up. According to this link http://msdn.microsoft.com/en-us/library/cc853327(v=vs.100).aspx “Loading Metadata” carries a moderate time cost but only needs to occur once per AppDomain. I have found no pre-compilation like tricks for loading the meta-data. The workaround … Read more

How can I prevent EF “The context cannot be used while the model is being created” errors?

I finally figured out the true cause of this, at least for me. The issue was that I was retrieving a DbContext from Windsor in my custom Asp.Net Membership provider. This caused an issue because the membership provider has a lifespan of the whole application, while all other retrieval calls for the db context were … Read more

Cleanest Way To Map Entity To DTO With Linq Select?

Just use AutoMapper. Example: Mapper.CreateMap<Address, AddressDTO>(); Mapper.CreateMap<Person, PersonDTO>(); Your query will execute when the mapping is performed but if there are fields in the entity that you’re not interested use Project().To<> which is available both for NHibernate and EntityFramework. It will effectively do a select on the fields specified in the mapping configurations.