├── .github
├── database.jpg
├── folderstructure.jpg
└── postmandatabase.jpg
├── .gitignore
├── .vs
└── config
│ └── applicationhost.config
├── AspnetCoreEFCoreExample.sln
├── README.md
└── src
└── AspnetCoreEFCoreExample
├── AspnetCoreEFCoreExample.csproj
├── AspnetCoreEFCoreExample.user
├── Controllers
└── MyModelController.cs
├── DataBaseContext.cs
├── Migrations
├── 20160703184938_Initial.Designer.cs
├── 20160703184938_Initial.cs
└── DataBaseContextModelSnapshot.cs
├── Models
├── MyModel.cs
└── MyModelViewModel.cs
├── Program.cs
├── Project_Readme.html
├── Properties
└── launchSettings.json
├── Repositories
├── ExampleRepository.cs
└── IExampleRepository.cs
├── Startup.cs
├── appsettings.json
├── web.config
└── wwwroot
└── web.config
/.github/database.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FabianGosebrink/ASPNETCore-Entity-Framework-Core/efcf435ae3b9280dc941b4dcdba2a4ad2f96f4bd/.github/database.jpg
--------------------------------------------------------------------------------
/.github/folderstructure.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FabianGosebrink/ASPNETCore-Entity-Framework-Core/efcf435ae3b9280dc941b4dcdba2a4ad2f96f4bd/.github/folderstructure.jpg
--------------------------------------------------------------------------------
/.github/postmandatabase.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FabianGosebrink/ASPNETCore-Entity-Framework-Core/efcf435ae3b9280dc941b4dcdba2a4ad2f96f4bd/.github/postmandatabase.jpg
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | src/AspnetCoreEFCoreExample/bin
2 | src/AspnetCoreEFCoreExample/obj
3 | .vs/
4 |
--------------------------------------------------------------------------------
/.vs/config/applicationhost.config:
--------------------------------------------------------------------------------
1 |
2 |
20 |
21 |
22 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 |
472 |
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 |
483 |
484 |
485 |
486 |
487 |
488 |
489 |
490 |
491 |
492 |
493 |
494 |
495 |
496 |
497 |
498 |
499 |
500 |
501 |
502 |
503 |
504 |
505 |
506 |
507 |
508 |
509 |
510 |
511 |
512 |
513 |
514 |
515 |
516 |
517 |
518 |
519 |
520 |
521 |
522 |
523 |
524 |
525 |
526 |
527 |
528 |
529 |
530 |
531 |
532 |
533 |
534 |
535 |
536 |
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 |
545 |
546 |
547 |
548 |
549 |
550 |
551 |
552 |
553 |
554 |
555 |
556 |
557 |
558 |
559 |
560 |
561 |
562 |
563 |
564 |
565 |
566 |
567 |
568 |
569 |
570 |
571 |
572 |
573 |
574 |
575 |
576 |
577 |
578 |
579 |
580 |
581 |
582 |
583 |
584 |
585 |
586 |
587 |
588 |
589 |
590 |
591 |
592 |
593 |
594 |
595 |
596 |
597 |
598 |
599 |
600 |
601 |
602 |
603 |
604 |
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 |
613 |
614 |
615 |
616 |
617 |
618 |
619 |
620 |
621 |
622 |
623 |
624 |
625 |
626 |
627 |
628 |
629 |
630 |
631 |
632 |
633 |
634 |
635 |
636 |
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
646 |
647 |
648 |
649 |
650 |
651 |
652 |
653 |
654 |
655 |
656 |
657 |
658 |
659 |
660 |
661 |
662 |
663 |
664 |
665 |
666 |
667 |
668 |
669 |
670 |
671 |
672 |
673 |
674 |
675 |
676 |
677 |
678 |
679 |
680 |
681 |
682 |
683 |
684 |
685 |
686 |
687 |
688 |
689 |
690 |
691 |
692 |
693 |
694 |
695 |
696 |
697 |
698 |
699 |
700 |
701 |
702 |
703 |
704 |
705 |
706 |
707 |
708 |
709 |
710 |
711 |
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
720 |
721 |
722 |
723 |
724 |
725 |
726 |
727 |
728 |
729 |
730 |
731 |
732 |
733 |
734 |
735 |
736 |
737 |
738 |
739 |
740 |
741 |
742 |
743 |
744 |
745 |
746 |
747 |
748 |
749 |
750 |
751 |
752 |
753 |
754 |
755 |
756 |
757 |
758 |
759 |
760 |
761 |
762 |
763 |
764 |
765 |
766 |
767 |
768 |
769 |
770 |
771 |
772 |
773 |
774 |
775 |
776 |
777 |
778 |
779 |
780 |
781 |
782 |
783 |
784 |
785 |
786 |
787 |
788 |
789 |
790 |
791 |
792 |
793 |
794 |
795 |
796 |
797 |
798 |
799 |
800 |
801 |
802 |
803 |
804 |
805 |
806 |
807 |
808 |
809 |
810 |
811 |
812 |
813 |
814 |
815 |
816 |
817 |
818 |
819 |
820 |
821 |
822 |
823 |
824 |
825 |
826 |
827 |
828 |
829 |
830 |
831 |
832 |
833 |
834 |
835 |
836 |
837 |
838 |
839 |
840 |
841 |
842 |
843 |
844 |
845 |
846 |
847 |
848 |
849 |
850 |
851 |
852 |
853 |
854 |
855 |
856 |
857 |
858 |
859 |
860 |
861 |
862 |
863 |
864 |
865 |
866 |
867 |
868 |
869 |
870 |
871 |
872 |
873 |
874 |
875 |
876 |
877 |
878 |
879 |
880 |
881 |
882 |
883 |
884 |
885 |
886 |
887 |
888 |
889 |
890 |
891 |
892 |
895 |
896 |
897 |
898 |
899 |
900 |
901 |
902 |
903 |
904 |
905 |
906 |
907 |
908 |
911 |
912 |
913 |
914 |
915 |
916 |
917 |
918 |
919 |
920 |
921 |
922 |
923 |
924 |
925 |
926 |
927 |
928 |
929 |
930 |
931 |
932 |
933 |
934 |
935 |
936 |
937 |
938 |
939 |
940 |
941 |
942 |
943 |
944 |
945 |
946 |
947 |
948 |
949 |
950 |
951 |
952 |
953 |
954 |
955 |
956 |
957 |
958 |
959 |
960 |
961 |
962 |
963 |
964 |
965 |
966 |
967 |
968 |
969 |
970 |
971 |
972 |
973 |
974 |
975 |
976 |
977 |
978 |
979 |
980 |
981 |
982 |
983 |
984 |
985 |
986 |
987 |
988 |
989 |
990 |
991 |
992 |
993 |
994 |
995 |
996 |
997 |
998 |
999 |
1000 |
1001 |
1002 |
1003 |
1004 |
1005 |
1006 |
1007 |
1008 |
1009 |
1010 |
1011 |
1012 |
1013 |
1014 |
1015 |
1016 |
1017 |
1018 |
1019 |
1020 |
1021 |
1022 |
1023 |
1024 |
1025 |
1026 |
1027 |
1028 |
1029 |
1030 |
1031 |
1032 |
1033 |
1034 |
1035 |
1036 |
1037 |
1038 |
1039 |
1040 |
--------------------------------------------------------------------------------
/AspnetCoreEFCoreExample.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27004.2006
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{29B073DF-CA65-4AF0-9B3D-BA36E724593A}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{CFA0172C-5BDA-4B4E-8252-4668C5934396}"
9 | EndProject
10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspnetCoreEFCoreExample", "src\AspnetCoreEFCoreExample\AspnetCoreEFCoreExample.csproj", "{F7153EB6-5B6C-4095-AF60-ADC40647B147}"
11 | EndProject
12 | Global
13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
14 | Debug|Any CPU = Debug|Any CPU
15 | Release|Any CPU = Release|Any CPU
16 | EndGlobalSection
17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
18 | {F7153EB6-5B6C-4095-AF60-ADC40647B147}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19 | {F7153EB6-5B6C-4095-AF60-ADC40647B147}.Debug|Any CPU.Build.0 = Debug|Any CPU
20 | {F7153EB6-5B6C-4095-AF60-ADC40647B147}.Release|Any CPU.ActiveCfg = Release|Any CPU
21 | {F7153EB6-5B6C-4095-AF60-ADC40647B147}.Release|Any CPU.Build.0 = Release|Any CPU
22 | EndGlobalSection
23 | GlobalSection(SolutionProperties) = preSolution
24 | HideSolutionNode = FALSE
25 | EndGlobalSection
26 | GlobalSection(NestedProjects) = preSolution
27 | {F7153EB6-5B6C-4095-AF60-ADC40647B147} = {29B073DF-CA65-4AF0-9B3D-BA36E724593A}
28 | EndGlobalSection
29 | GlobalSection(ExtensibilityGlobals) = postSolution
30 | SolutionGuid = {B0F0AB46-D9F9-49E2-ACC4-D00BD68BF404}
31 | EndGlobalSection
32 | EndGlobal
33 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ASP.NET Core with Entity Framework Core
2 |
3 | In this repository I want to show the first steps with Entity Framework Core and ASP.NET Core.
4 |
5 | The Repository contains a sample WebAPI with CRUD-Operations that you can Get/Read/Update and Delete "MyModel" objects through HTTP-calls.
6 |
7 | ## Before starting...
8 |
9 | Please read this readme and think of executing ``` Add-Migration [MyMigrationName]``` and ```update-database``` in the Package Manager Console first.
10 |
11 | The database should now be visible inside your SQL Server Management Studio.
12 |
13 | 
14 |
15 | 
16 |
17 | ## Run it...
18 |
19 | If you now run the solution you can perform CRUD operations for example via postman, angular etc...
20 |
21 | 
22 |
23 | [ASP.NET Core Application to New Database](https://ef.readthedocs.io/en/latest/platforms/aspnetcore/new-db.html)
24 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/AspnetCoreEFCoreExample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.0
5 |
6 |
7 |
8 |
9 | PreserveNewest
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | All
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/AspnetCoreEFCoreExample.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | IIS Express
5 |
6 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Controllers/MyModelController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Net;
4 | using AspnetCoreEFCoreExample.Models;
5 | using AspnetCoreEFCoreExample.Repositories;
6 | using AutoMapper;
7 | using Microsoft.AspNetCore.Mvc;
8 |
9 | namespace AspnetCoreEFCoreExample.Controllers
10 | {
11 | [Route("api/[controller]")]
12 | public class MyModelController : Controller
13 | {
14 | private readonly IExampleRepository _exampleRepository;
15 |
16 | public MyModelController(IExampleRepository exampleRepository)
17 | {
18 | _exampleRepository = exampleRepository;
19 | }
20 |
21 | // GET: api/mymodel
22 | [HttpGet("", Name = "GetAll")]
23 | public IActionResult Get()
24 | {
25 | try
26 | {
27 | return Ok(_exampleRepository.GetAll().Select(x => Mapper.Map(x)));
28 | }
29 | catch (Exception exception)
30 | {
31 | //logg exception or do anything with it
32 | return StatusCode((int)HttpStatusCode.InternalServerError);
33 | }
34 | }
35 |
36 | // GET api/values/5
37 | [HttpGet("{id}", Name = "GetSingle")]
38 | public IActionResult Get(int id)
39 | {
40 | try
41 | {
42 | MyModel myModel = _exampleRepository.GetSingle(id);
43 |
44 | if (myModel == null)
45 | {
46 | return NotFound();
47 | }
48 |
49 | return Ok(Mapper.Map(myModel));
50 | }
51 | catch (Exception exception)
52 | {
53 | //Do something with the exception
54 | return StatusCode((int)HttpStatusCode.InternalServerError);
55 | }
56 | }
57 |
58 | // POST api/values
59 | [HttpPost]
60 | public IActionResult Post([FromBody]MyModelViewModel viewModel)
61 | {
62 | try
63 | {
64 | if (viewModel == null)
65 | {
66 | return BadRequest();
67 | }
68 |
69 | if (!ModelState.IsValid)
70 | {
71 | return BadRequest(ModelState);
72 | }
73 |
74 | MyModel item = Mapper.Map(viewModel);
75 |
76 | _exampleRepository.Add(item);
77 | int save = _exampleRepository.Save();
78 |
79 | if (save > 0)
80 | {
81 | return CreatedAtRoute("GetSingle", new { controller = "MyModel", id = item.Id }, item);
82 | }
83 |
84 | return BadRequest();
85 | }
86 | catch (Exception exception)
87 | {
88 | //Do something with the exception
89 | return StatusCode((int)HttpStatusCode.InternalServerError);
90 | }
91 | }
92 |
93 | // PUT api/values/5
94 | [HttpPut("{id}")]
95 | public IActionResult Put(int id, [FromBody]MyModelViewModel viewModel)
96 | {
97 | try
98 | {
99 | if (viewModel == null)
100 | {
101 | return BadRequest();
102 | }
103 |
104 | if (!ModelState.IsValid)
105 | {
106 | return BadRequest(ModelState);
107 | }
108 |
109 | MyModel singleById = _exampleRepository.GetSingle(id);
110 |
111 | if (singleById == null)
112 | {
113 | return NotFound();
114 | }
115 |
116 | singleById.Name = viewModel.Name;
117 |
118 | _exampleRepository.Update(singleById);
119 | int save = _exampleRepository.Save();
120 |
121 | if (save > 0)
122 | {
123 | return Ok(Mapper.Map(singleById));
124 | }
125 |
126 | return BadRequest();
127 | }
128 | catch (Exception exception)
129 | {
130 | //Do something with the exception
131 | return StatusCode((int)HttpStatusCode.InternalServerError);
132 | }
133 | }
134 |
135 | // DELETE api/values/5
136 | [HttpDelete("{id}")]
137 | public IActionResult Delete(int id)
138 | {
139 | try
140 | {
141 | MyModel singleById = _exampleRepository.GetSingle(id);
142 |
143 | if (singleById == null)
144 | {
145 | return NotFound();
146 | }
147 |
148 | _exampleRepository.Delete(singleById);
149 | int save = _exampleRepository.Save();
150 |
151 | if (save > 0)
152 | {
153 | return NoContent();
154 | }
155 |
156 | return BadRequest();
157 | }
158 | catch (Exception exception)
159 | {
160 | //Do something with the exception
161 | return StatusCode((int)HttpStatusCode.InternalServerError);
162 | }
163 | }
164 | }
165 | }
166 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/DataBaseContext.cs:
--------------------------------------------------------------------------------
1 | using AspnetCoreEFCoreExample.Models;
2 | using Microsoft.EntityFrameworkCore;
3 |
4 | namespace AspnetCoreEFCoreExample
5 | {
6 | public class DataBaseContext : DbContext
7 | {
8 | public DbSet MyModels { get; set; }
9 |
10 | public DataBaseContext(DbContextOptions options)
11 | : base(options)
12 | { }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Migrations/20160703184938_Initial.Designer.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using Microsoft.EntityFrameworkCore.Infrastructure;
3 | using Microsoft.EntityFrameworkCore.Metadata;
4 | using Microsoft.EntityFrameworkCore.Migrations;
5 |
6 | namespace AspnetCoreEFCoreExample.Migrations
7 | {
8 | [DbContext(typeof(DataBaseContext))]
9 | [Migration("20160703184938_Initial")]
10 | partial class Initial
11 | {
12 | protected override void BuildTargetModel(ModelBuilder modelBuilder)
13 | {
14 | modelBuilder
15 | .HasAnnotation("ProductVersion", "1.0.0-rtm-21431")
16 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
17 |
18 | modelBuilder.Entity("AspnetCoreEF7Example.Models.MyModel", b =>
19 | {
20 | b.Property("Id")
21 | .ValueGeneratedOnAdd();
22 |
23 | b.Property("Name");
24 |
25 | b.HasKey("Id");
26 |
27 | b.ToTable("MyModels");
28 | });
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Migrations/20160703184938_Initial.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore.Metadata;
2 | using Microsoft.EntityFrameworkCore.Migrations;
3 |
4 | namespace AspnetCoreEFCoreExample.Migrations
5 | {
6 | public partial class Initial : Migration
7 | {
8 | protected override void Up(MigrationBuilder migrationBuilder)
9 | {
10 | migrationBuilder.CreateTable(
11 | name: "MyModels",
12 | columns: table => new
13 | {
14 | Id = table.Column(nullable: false)
15 | .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
16 | Name = table.Column(nullable: true)
17 | },
18 | constraints: table =>
19 | {
20 | table.PrimaryKey("PK_MyModels", x => x.Id);
21 | });
22 | }
23 |
24 | protected override void Down(MigrationBuilder migrationBuilder)
25 | {
26 | migrationBuilder.DropTable(
27 | name: "MyModels");
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Migrations/DataBaseContextModelSnapshot.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using Microsoft.EntityFrameworkCore.Infrastructure;
3 | using Microsoft.EntityFrameworkCore.Metadata;
4 |
5 | namespace AspnetCoreEFCoreExample.Migrations
6 | {
7 | [DbContext(typeof(DataBaseContext))]
8 | partial class DataBaseContextModelSnapshot : ModelSnapshot
9 | {
10 | protected override void BuildModel(ModelBuilder modelBuilder)
11 | {
12 | modelBuilder
13 | .HasAnnotation("ProductVersion", "1.0.0-rtm-21431")
14 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
15 |
16 | modelBuilder.Entity("AspnetCoreEF7Example.Models.MyModel", b =>
17 | {
18 | b.Property("Id")
19 | .ValueGeneratedOnAdd();
20 |
21 | b.Property("Name");
22 |
23 | b.HasKey("Id");
24 |
25 | b.ToTable("MyModels");
26 | });
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Models/MyModel.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 |
3 | namespace AspnetCoreEFCoreExample.Models
4 | {
5 | public class MyModel
6 | {
7 | [Key]
8 | public int Id { get; set; }
9 | public string Name { get; set; }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Models/MyModelViewModel.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 |
3 | namespace AspnetCoreEFCoreExample.Models
4 | {
5 | public class MyModelViewModel
6 | {
7 | public int Id { get; set; }
8 | [Required]
9 | public string Name { get; set; }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Program.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Hosting;
2 | using Microsoft.AspNetCore;
3 | using Microsoft.Extensions.Configuration;
4 |
5 | namespace AspnetCoreEFCoreExample
6 | {
7 | public class Program
8 | {
9 | public static void Main(string[] args)
10 | {
11 | BuildWebHost(args).Run();
12 | }
13 |
14 | public static IWebHost BuildWebHost(string[] args) =>
15 | WebHost.CreateDefaultBuilder(args)
16 | .ConfigureAppConfiguration((hostContext, config) =>
17 | {
18 | // delete all default configuration providers
19 | config.Sources.Clear();
20 | config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
21 | config.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
22 | })
23 | .UseStartup()
24 | .Build();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Project_Readme.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Welcome to ASP.NET 5
6 |
127 |
128 |
129 |
130 |
138 |
139 |
140 |
141 |
This application consists of:
142 |
143 | - Sample pages using ASP.NET MVC 6
144 | - Gulp and Bower for managing client-side libraries
145 | - Theming using Bootstrap
146 |
147 |
148 |
160 |
161 |
Overview
162 |
171 |
172 |
173 |
Run & Deploy
174 |
180 |
181 |
182 |
185 |
186 |
187 |
188 |
189 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "iisSettings": {
3 | "windowsAuthentication": false,
4 | "anonymousAuthentication": true,
5 | "iisExpress": {
6 | "applicationUrl": "http://localhost:2486/",
7 | "sslPort": 0
8 | }
9 | },
10 | "profiles": {
11 | "IIS Express": {
12 | "commandName": "IISExpress",
13 | "launchBrowser": true,
14 | "launchUrl": "api/mymodel",
15 | "environmentVariables": {
16 | "ASPNETCORE_ENVIRONMENT": "Development"
17 | }
18 | },
19 | "AspnetCoreEFCoreExample": {
20 | "commandName": "Project",
21 | "launchBrowser": true,
22 | "launchUrl": "http://localhost:5000/api/mymodel",
23 | "environmentVariables": {
24 | "ASPNETCORE_ENVIRONMENT": "Development"
25 | }
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Repositories/ExampleRepository.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using AspnetCoreEFCoreExample.Models;
4 |
5 | namespace AspnetCoreEFCoreExample.Repositories
6 | {
7 | public class ExampleRepository : IExampleRepository
8 | {
9 | private readonly DataBaseContext _ctx;
10 |
11 | public ExampleRepository(DataBaseContext ctx)
12 | {
13 | _ctx = ctx;
14 | }
15 |
16 | public IEnumerable GetAll()
17 | {
18 | return _ctx.MyModels;
19 | }
20 |
21 | public MyModel GetSingle(int id)
22 | {
23 | return _ctx.MyModels.FirstOrDefault(x => x.Id == id);
24 | }
25 |
26 | public MyModel Add(MyModel toAdd)
27 | {
28 | _ctx.MyModels.Add(toAdd);
29 | return toAdd;
30 | }
31 |
32 | public MyModel Update(MyModel toUpdate)
33 | {
34 | _ctx.MyModels.Update(toUpdate);
35 | return toUpdate;
36 | }
37 |
38 | public void Delete(MyModel toDelete)
39 | {
40 | _ctx.MyModels.Remove(toDelete);
41 | }
42 |
43 | public int Save()
44 | {
45 | return _ctx.SaveChanges();
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Repositories/IExampleRepository.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using AspnetCoreEFCoreExample.Models;
3 |
4 | namespace AspnetCoreEFCoreExample.Repositories
5 | {
6 | public interface IExampleRepository
7 | {
8 | IEnumerable GetAll();
9 | MyModel GetSingle(int id);
10 | MyModel Add(MyModel toAdd);
11 | MyModel Update(MyModel toUpdate);
12 | void Delete(MyModel toDelete);
13 | int Save();
14 | }
15 | }
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/Startup.cs:
--------------------------------------------------------------------------------
1 | using AspnetCoreEFCoreExample.Models;
2 | using AspnetCoreEFCoreExample.Repositories;
3 | using Microsoft.AspNetCore.Builder;
4 | using Microsoft.AspNetCore.Hosting;
5 | using Microsoft.EntityFrameworkCore;
6 | using Microsoft.Extensions.Configuration;
7 | using Microsoft.Extensions.DependencyInjection;
8 | using Microsoft.Extensions.Logging;
9 |
10 | namespace AspnetCoreEFCoreExample
11 | {
12 | public class Startup
13 | {
14 | public Startup(IConfiguration configuration)
15 | {
16 | Configuration = configuration;
17 | }
18 |
19 | public IConfiguration Configuration { get; set; }
20 |
21 | // This method gets called by the runtime. Use this method to add services to the container.
22 | public void ConfigureServices(IServiceCollection services)
23 | {
24 | var configurationSection = Configuration.GetSection("ConnectionStrings:DefaultConnection");
25 | services.AddDbContext(options => options.UseSqlServer(configurationSection.Value));
26 | // Add framework services.
27 |
28 | services.AddMvc();
29 | services.AddScoped();
30 | }
31 |
32 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
33 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
34 | {
35 | AutoMapper.Mapper.Initialize(config =>
36 | {
37 | config.CreateMap().ReverseMap();
38 | });
39 |
40 | loggerFactory.AddConsole(Configuration.GetSection("Logging"));
41 | loggerFactory.AddDebug();
42 |
43 | app.UseDefaultFiles();
44 | app.UseStaticFiles();
45 |
46 | app.UseMvc();
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "IncludeScopes": false,
4 | "LogLevel": {
5 | "Default": "Debug",
6 | "System": "Information",
7 | "Microsoft": "Information"
8 | }
9 | },
10 | "ConnectionStrings": {
11 | "DefaultConnection": "Server=localhost\\SQLEXPRESS;Database=AspnetCoreEFCoreExample;Trusted_Connection=True;MultipleActiveResultSets=true;"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/web.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/AspnetCoreEFCoreExample/wwwroot/web.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------