generics

How can I return the ID of the Inserted Record in Entity Framework Generic Insert Method?

主宰稳场 提交于 2020-08-27 12:57:06
问题 Here is the generic insert method. I need your suggestion to return the ID of the inserted record. public static void Create<T>(T entity) where T : class { using (var context = new InformasoftEntities()) { DbSet dbSet = context.Set<T>(); dbSet.Add(entity); context.SaveChanges(); } } 回答1: You need a little modification: You need to create an IHasAutoID that implemented by Entity public interface IHasAutoID { int getAutoId(); } In Entity Class public class EntityA : IHasAutoID { public int

How can I return the ID of the Inserted Record in Entity Framework Generic Insert Method?

与世无争的帅哥 提交于 2020-08-27 12:45:12
问题 Here is the generic insert method. I need your suggestion to return the ID of the inserted record. public static void Create<T>(T entity) where T : class { using (var context = new InformasoftEntities()) { DbSet dbSet = context.Set<T>(); dbSet.Add(entity); context.SaveChanges(); } } 回答1: You need a little modification: You need to create an IHasAutoID that implemented by Entity public interface IHasAutoID { int getAutoId(); } In Entity Class public class EntityA : IHasAutoID { public int

Typescript: Force Default Generic Type to be `any` instead of `{}`

点点圈 提交于 2020-08-27 04:48:11
问题 I have a function a that should returns any if no generic type is provided, T otherwise. var a = function<T>() : T { return null; } var b = a<number>(); //number var c = a(); //c is {}. Not what I want... I want c to be any. var d; //any var e = a<typeof d>(); //any Is it possible? (Without changing the function calls obviously. AKA without a<any>() .) 回答1: Is it possible? (Without changing the function calls obviously. AKA without a().) Yes. I believe in your case you would do var a =

Typescript: Force Default Generic Type to be `any` instead of `{}`

て烟熏妆下的殇ゞ 提交于 2020-08-27 04:47:10
问题 I have a function a that should returns any if no generic type is provided, T otherwise. var a = function<T>() : T { return null; } var b = a<number>(); //number var c = a(); //c is {}. Not what I want... I want c to be any. var d; //any var e = a<typeof d>(); //any Is it possible? (Without changing the function calls obviously. AKA without a<any>() .) 回答1: Is it possible? (Without changing the function calls obviously. AKA without a().) Yes. I believe in your case you would do var a =